NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC24-CS105 (July-2024 24A)
PROGRAMMING IN JAVA
Assignment 3
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 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. Sta c Method
b. Throws a NullPointerExcep on
c. Compile‐ me error
d. Run me error
Correct Answer:
a. Sta c Method
Detailed Solu on:
The provided Java code will compile and execute successfully without any excep ons. When calling a
sta c method, it doesn't require an instance of the class. Therefore, you can call the sta c
method staticMethod() from class First using the null reference first.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 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
Correct Answer:
b. error: cannot assign a value to final variable a
Detailed Solu on:
Since the variable 'a' is declared as final, therefore, it can be accessed in the subclass however, its value
cannot be changed in the subclass. Therefore, the output will be error: cannot assign a value to final
variable a.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
All the variables of interface should be?
a. default and final
b. default and sta c
c. public, sta c and final
d. protect, sta c and final
Correct Answer:
c. public, sta c and final
Detailed Solu on:
Variables of an interface are public, sta c and final by default because the interfaces cannot be
instan ated, final ensures the value assigned cannot be changed with the implemen ng class and public
for it to be accessible by all the implemen ng classes.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 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
Correct Answer:
c. 7 9
Detailed Solu on:
x and y are sta c variables, so they are shared across all instances of the class sta c_out.
When obj1.add(a, a + 1) is called, x and y are updated to 5 and 8, respec vely.
When obj2.add(5, a) is called, x and y are updated to 7 and 9, respec vely.
The final values of x and y a er all method calls are 7 and 9, respec vely, which are printed by
System.out.println(obj1.x + " " + obj2.y);.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 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. Run me Error
d. Compila on Error
Correct Answer:
b. 3 3
Detailed Solu on:
The first 3 is printed by System.out.println(obj.x); because x was set to 3 (2 + 1) in the cal method.
The second 3 is printed by obj.print(); because y was set to 3 in the cal method.
Although y is a private variable, it is s ll accessible within the methods of the same class. Therefore, the
print method can access and print its value.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 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
Correct Answer:
b. Its value cannot be changed
Detailed Solu on:
A final variable of a primi ve data type cannot change its value once it has been ini alize.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 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
Correct Answer:
c. Private members
Detailed Solu on:
Private access specifier is the most secure access mode. It doesn’t allow members to be inherited. Even
Private inheritance can only inherit protected and public members.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 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
Correct Answer:
a. Public and Protected members of base class becomes protected members of derived class.
Detailed Solu on:
As the programming language rules apply, all the public and protected members of base class becomes
protected members of derived class in protected access mode. It can’t be changed because it would
hinder the security of data and may add vulnerability in the program.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Which type of inheritance leads to diamond problem?
a. Single level
b. Mul ‐level
c. Mul ple
d. Hierarchical
Correct Answer:
c. Mul ple
Detailed Solu on:
When 2 or more classes inherit the same class using mul ple inheritance and then one more class
inherits those two base classes, we get a diamond like structure. Here, ambiguity arises when same
func on gets derived into 2 base classes and finally to 3rd level class because same name func ons are
being inherited.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 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
Correct Answer:
a. error: func() in subDemoClass cannot override func() in superDemoClass
Detailed Solu on:
Here in this program, the subclass is trying to override the final method of the superclass, i.e. it is trying
to change the behavior of the final method. The behavior of the final method cannot be changed in the
subclass. In other words, the final method cannot be overridden in any subclass because the final
method is a complete method. Therefore,
error: func() in subDemoClass cannot override func() in superDemoClass