NPTEL Programming In Java Week 5 Assignment Answers 2022

 Q1. Consider the following program.

class Question{
static int b =2;
}
class Answer extends Question{ static int b =20;
}
public class Questionl extends Answer{
public static void main(String args[]) {
b =100;
System.out.println (Answer.b);
System.out.println (b+Question.b);
}
}

If the program is executed, then what will be the output from the execution?

a. 100

102
b. 20

22
c. 102

100

d. 22

20

Answer: Option A

Q2. Consider the following program.

class Question {
int a=40;
int b=20;
}
public class Childl extends Question {
int a=100;
int b=200;
void add (int a, int b) {
System.out.println(a+this.b-super.a);
}
public static void main(String[] args) { Childl c = new Childl();
c.add(10,30);
}
}

If the program is executed, then what will be the output from the execution?

a. 170

b. 130

c. 0

d. 260

Answer: a. 170


Q3. What is the output of the following code?

class Explanation{
public void Print () {
System.out.println("This is Explanation's Print method");
}
}
class Answer extends Explanation{
public void Print () { 
   super.super. Print ();
 System.out.println("This is Answer's Print method"); 
}

}
public class Question{
 public static void main(String[] args) {
 Answer a new Answer(); 
a. Print();
}
}

a. Output: This is Explanation’s Print method

This is Answer’s Print method

b. Error: ‘super.super‘ is not allowed.

c. Error: Compilation unsuccessful, as there is only one super class of Answer.

d. Output: This is Answer’s Print method

This is Explanation’s Print method

Answer: b. Error: ‘super.super‘ is not allowed.

Q4. Which of the following is/are interface(s)?

a. DriverManager

b. Connection

c. Statement

d. ResultSet

Answer: b. Connection

c. Statement

d. ResultSet


Q5. Which of the following statement(s) is/are true?

a. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.

b. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.

c. A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in.

d. You cannot declare new methods in the subclass that are not in the superclass.

Answer: a. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.

b. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.

c. A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in.

Q6. Which of the following statement(s) is/are true?

a. Static methods in interfaces are never inherited.

b. You will get a compile-time error if you attempt to change an instance method in the superclass to a static method in the subclass.

c. You can prevent a class from being sub classed by using the final keyword in the class’s declaration.

d. An abstract class can only be subclassed; it cannot be instantiated.

Answer: Option A, B, C, D.

Q7. Consider the following piece of code.

interface A{
int i = 22;
}
public class B implements A {
void methodB () {
i = 2;
System.out.println(i);
}
}

Which of the following statement(s) is/are correct?

a. There is no main () method so the program is not compile successfully.

b. The value of i will be printed as 22, as it is static and final by default.

c. The value of i will be printed as 2, as it is initialized in class B.

d. Compile time error.

Answer: a. There is no main () method so the program is not compile successfully.


Q8. Which of the following statement(s) is/are true?

a. A class can extend more than one class.

b. An interface can extend many interfaces.

c. An interface can implement many interfaces.

d. A class can extend one class and implement many interfaces.

Answer: b. An interface can extend many interfaces.

d. A class can extend one class and implement many interfaces.

Q9. All classes in Java are inherited from which class?

a. java.lang.class

b. java.class.inherited

c. java.class.object

d. java.lang.Object

Answer: d. java.lang.Object


Q10.

abstract class C1{
public C1(){
System.out.print(10);
}
}
class C2 extends C1 {
public C2(){
System.out.print(20);
}
}
public class Test {
public static void main(String[] a){
new C1();
}
}

If the program is executed, then what will be the output from the execution?

a. Output: 1020

b. Output: 30

c. Output: 2010

d. Error: C1 is abstract; cannot be instantiated.

Answer: d. Error: C1 is abstract; cannot be instantiated.


Not 100% confirm

Comments

Popular posts from this blog

Walmart Sparkplug 2022 | 150 students will be selected for summer internship | ₹1-1.1 lakh monthly

Flipkart Runway: Season 3 | 2025 | Internship

Python for Data Science NPTEL Assignment Solutions Week 4 2022

GATE 2022 Answer Key Release Date Announced; Details Here