Programming in Java Assignment3: NPTEL Online Certification Courses Indian Institute of Technology Kharagpur
Programming in Java Assignment3: NPTEL Online Certification Courses Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment3
TYPE OF QUESTION: MCQ
Number of questions:10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
Which of following statement(s) is/are true?
a. Instance methods can access instance variables and instance methods directly.
b. Instance methods cannot access class variables and class methods directly.
c. Class methods can access class variables and class methods directly.
d. Class methods cannot access instance variables or instance methods directly—they must
use an object reference.
Correct Answer: a, c, d
Detailed Solution:
Instance methods can access class variables and class methods directly.
______________________________________________________________________________
QUESTION 2:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. 6
b. 10
c. 21
d. runtime error
Correct Answer: a
Detailed Solution:
Because x is defined as a public static int in the class Question, every reference to x will have
the value that was last assigned because x is a static variable (and therefore a class variable)
shared across all instances of the class. That is, there is only one x: when the value of x changes
in any instance it affects the value of x for all instances of Question.
______________________________________________________________________________
QUESTION 3:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Which of the following is called when a method having the same name as that of the class is
defined?
a. abstract
b. this
c. final
d. constructor
Correct Answer: d
Detailed Solution:
In a class, if more than one method having the same name but with different signature is used,
then it is called a constructor.
______________________________________________________________________
QUESTION 4:
Hiding internal data from the outside world, and accessing it only through publicly exposed
methods is known as data_______________
a. Polymorphism
b. Encapsulation.
c. Inheritance.
d. Duplication.
Correct Answer: b
Detailed Solution: Hiding the internal details from the outside world is known as encapsulation.
______________________________________________________________________________
QUESTION 5:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
class B {
protected int method(int a, int b){
return 0;
}
}
Which of the following method(s) is/are declaration is/are not valid in a class that extends class B?
Detailed Solution:
Option a is correct because the class that extends A is just simply overriding method.
Others are not.
________________________________________________________________
QUESTION 6:
Which of the following statement(s) is/are true?
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
A final method cannot be overridden in a subclass. Class methods cannot use this keyword as
there is no instance for this to refer to. The advantage of private static methods is that they can be
reused later if you need to reinitialize the class variable.
______________________________________________________________________________
QUESTION 7:
Advantage(s) of inheritance in Java programming is/are
a. Code sharing
b. Code maintainability
c. Code reusability
d. All of the above
Correct Answer: d
Detailed Solution:
In fact, above benefits are related to each other. Frequent use of inheritance in Java language is for deriving
classes from existing classes that provides reusability. In simple terms, once we have written a class then it
can be extended or sub classed without changing the code of base class.
___________________________________________________________________________
QUESTION 8:
Consider the following class definition:
class Student extends String {
Correct Answer: c
_____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Correct Answer: b, d
Detailed Solution:
Constructor if you defined recursively, then it will show runtime error.
_____________________________________________________________________________
QUESTION 10:
Which of the following statement(s) is/are true?
a. Hiding internal data from the outside world, and accessing it only through publicly
exposed methods is known as data encapsulation.
b. Common behavior can be defined in a superclass and inherited into a subclass using
the extends keyword.
c. The term "class variable" is another name for non-static field.
d. A local variable stores temporary state; it is declared inside a method.
Detailed Solution:
The term "class variable" is another name for static field.
______________________________________________________________________________