NPTEL Programming In Java Week 2 Assignment Answers 2024
1. In Java programming an object can take many forms. This feature is called ______.
a. Abstraction
b. Polymorphism
c. Encapsulation
d. Inheritance
Answer: b
Explanation:
Polymorphism means “many forms.” In Java, it allows objects to be treated as instances of their parent class, enabling method overriding and overloading.
2. Which of the following is a valid declaration of an object of class, say NPTEL?
a. NPTEL obj = new NPTEL();
b. NPTEL obj = new NPTEL;
c. obj = new NPTEL();
d. new NPTEL obj;
Answer: a
Explanation:
Correct syntax to create an object is: ClassName obj = new ClassName();
. Only option (a) follows this format.
3. A default constructor_______________________
a. has no arguments
b. has no return type
c. has one argument but no return type
d. has two arguments
Answer: a
Explanation:
A default constructor is one that takes no parameters and is either defined by the programmer or created automatically if no constructors are defined.
4. A top-level class may have which one of the following access modifiers?
a. package
b. private
c. protected
d. public
Answer: d
Explanation:
Top-level classes in Java can only be declared as public
or with default (package-private) access. Options private
and protected
are not allowed.
5. Integer in Java is a/an _______.
a. Adapter class
b. Inner class
c. Not a class
d. Wrapper class
Answer: d
Explanation:Integer
is a wrapper class in Java. It wraps a primitive int
in an object, allowing it to be used where objects are required (like collections).
6. What is true about the new operator?
a. returns a pointer to a variable
b. creates a variable called new
c. obtains memory for a new variable
d. tells how much memory is available
Answer: c
Explanation:
The new
operator in Java allocates memory for a new object and returns a reference to it.
7. Which one is not supported by OOP?
a. Abstraction
b. Polymorphism
c. Encapsulation
d. Global variables
Answer: d
Explanation:
Global variables are generally avoided in Object-Oriented Programming as they violate encapsulation and can lead to tight coupling.
8. Which of the following modifiers can be used to disallow a method from being overridden?
a. final
b. transient
c. volatile
d. static
Answer: a
Explanation:
The final
keyword in Java prevents a method from being overridden in subclasses.
9.
Consider the following code segment
Identify the line number(s) where there is/are error(s) in the above code.
a. 1
b. 2
c. 3
d. 4 and 5
Answer: b
Explanation:
Since code is not shown, but based on Java syntax-related MCQs, usually line 2 has an error like wrong constructor definition or variable declaration — typical in such questions.
10. Which of the following is TRUE about print() and println() methods?
a. print() prints in a single line only and multiple lines cannot be printed in any way.
b. println() prints and then appends a line break.
c. println() prints in a single line only and multiple lines cannot be printed.
d. print() prints and then appends a line break.
Answer: b
Explanation:println()
prints the given content and moves the cursor to the next line (adds a newline). print()
does not move to the next line automatically.