NPTEL Programming in Java Week 3 Assignment Answers 2024
1. What will be the output of the following program?
class First {
static void staticMethod() {
System.out.println("Static Method");
}
}
class MainClass {
public static void main(String[] args) {
First first = null;
First.staticMethod();
}
}
a. Static Method
b. Throws a NullPointerException
c. Compile-time error
d. Run time error
Answer :- For Answers Click Here
2. What will be the output of the below program.
class superDemoClass {
final int a = 20;
}
class subDemoClass extends superDemoClass {
void subFunc() {
a = 40;
System.out.println("value of a = " + a);
}
}
class demo {
public static void main(String[] args) {
subDemoClass subc = new subDemoClass();
subc.subFunc();
}
}
a. value of a = 20
b. error: cannot assign a value to final variable ‘a’
c. error: unknown variable ‘a’ in class subDemoClass
d. value of a = 40
Answer :- For Answers Click Here
3. All the variables of interface should be?
a. default and final
b. default and static
c. public, static and final
d. protect, static and final
Answer :-
4. What will be the output of the below program.
class static_out {
static int x;
static int y;
void add(int a, int b) {
x = a + b;
y = x + b;
}
}
public class static_use {
public static void main(String args[]) {
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
a. 7 7.4
b. 6 6.4
c. 7 9
d. 9 7
Answer :- For Answers Click Here
5. What will be the output of the following Java code?
class access {
public int x;
private int y;
void cal(int a, int b) {
x = a + 1;
y = b;
}
void print() {
System.out.println(" " + y);
}
}
public class access_specifier {
public static void main(String args[]) {
access obj = new access();
obj.cal(2, 3);
System.out.print(obj.x);
obj.print();
}
}
a. 2 3
b. 3 3
c. Runtime Error
d. Compilation Error
Answer :-
6. If a variable of primitive datatype in Java is declared as final, then
a. It cannot get inherited
b. Its value cannot be changed
c. It cannot be accessed in the subclass
d. All of the above
Answer :-
7. Members which are not intended to be inherited are declared as
a. Public members
b. Protected members
c. Private members
d. Private or Protected members
Answer :- For Answers Click Here
8. If a base class is inherited in protected access mode then which among the following is true?
a. Public and Protected members of base class becomes protected members of derived class
b. Only protected members become protected members of derived class
c. Private, Protected and Public all members of base, become private of derived class
d. Only private members of base, become private of derived class
Answer :-
9. Which type of inheritance leads to diamond problem?
a. Single level
b. Multi-level
c. Multiple
d. Hierarchical
Answer :-
10. What will be the output of the below program:
class superDemoClass {
final void func() {
int a = 20;
System.out.println("value of a = " + a);
}
}
class subDemoClass extends superDemoClass {
void func() {
int b = 60;
System.out.println("value of b = " + b);
}
}
class demo {
public static void main(String[] args) {
subDemoClass subc = new subDemoClass();
subc.func();
}
}
a. error: func() in subDemoClass cannot override func() in superDemoClass
b. value of b = 60
c. value of a = 20
d. None of the above
Answer :- For Answers Click Here