PROGRAMMING IN JAVA Assignment 3 | NPTEL | Answer with Explanation
Test Your Java Fundamentals with a Challenging MCQ Assignment on " Input-Output Handling in Java " Sharpen your core Java skills with this engaging multiple-choice quiz (MCQ). Designed for beginners, this Assignment 3 assesses your understanding of fundamental Java concepts. Whether you're new to programming or refreshing your knowledge, this MCQ [Input-Output Handling in Java ] challenge is a perfect way to gauge your progress and identify areas for improvement.
Which of the following statement(s) is/are correct about the constructor?
a. Constructors cannot be synchronized in Java.b. Java does not provide a default copy constructor.
c. A constructor cannot be overloaded.
d. “this” or “super” can be used in a constructor.
Answer
a. Constructors cannot be synchronized in Java.,b. Java does not provide a default copy constructor.,“this” or “super” can be used in a constructor.
We can overload constructors just like methods in Java.
Here's a breakdown of why these statements are true:
Synchronization: Constructors are inherently thread-safe because only one constructor can be executed at a time during object creation. Synchronization is not necessary.
Copy Constructor: Java doesn't provide a built-in default copy constructor. You can create your own copy constructor to explicitly copy an object's state.
Overloading Constructors: Java allows constructor overloading, where you can define multiple constructors with different parameter lists. This lets you initialize objects in various ways.
this and super: The keyword "this" is used within a constructor to refer to the current object being created. "super" is used to call the superclass constructor from a subclass constructor.
Learn Java Programming for free with Computer Course - CompEduBox Android app. Share with your Friends
Which of the following statement(s) is/are true?
a. You can write a new instance method in the subclass with the same signature as the one in the superclass, thus overriding it.b. You can write a new static method in the subclass with the same signature as the one in the superclass, thus hiding it.
c. A subclass inherits all of its parent's public and protected members, 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 with the same signature as the one
in the superclass, thus overriding it.,
b. You can write a new static method in the subclass with the same signature as the one in
the superclass, thus hiding it.,
c. A subclass inherits all of its parent's public and protected members, no matter what
package the subclass is in.
You can declare new methods in the subclass that are not in the superclass.
The distinction between hiding a static method and overriding an instance method has important
implications:
The version of the overridden instance method that gets invoked is in the subclass. The version of
the hidden static method invoked depends on whether it is invoked from the superclass or the
subclass.
Consider the following piece of code.
class Test{
void exam(){}
}
public class Test1 extends Test {
___________ void exam(){} // insert correct keyword
public static void main(String[] args) {
System.out.println(" Correct");
}
}
Fill in the blank with the appropriate keyword(s) from the list given below so that the program compiles successfully.
a. abstractb. final
c. default
d. public
Answer
b. final,d. public
final, public are the correct keyword in this case.
How many instances of abstract class can be created?
a. 0b. 1
c. 2
d. MUltiple
Answer
a. 0
An abstract class cannot be instantiated. So it will have 0 instances.
Structuring a Java class such that only methods within the class can access its instance variables is referred to as _______.
a. object orientationb. inheritance
c. platform independence
d. encapsulation
Answer
d. encapsulation
Encapsulation removes access to a class’s instance variables from processes outside the class,
making Option D the correct answer.
Which of the following statement(s) is/are true?
a. A final method cannot be overridden in a subclass.b. The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.
c. Class methods cannot use this keyword as there is no instance for this to refer to.
d. A final method can be overridden in a subclass.
Answer
a. A final method cannot be overridden in a subclass.,
b. The advantage of private static methods is that they can be reused later if you need to
reinitialize the class variable.,
c. Class methods cannot use this keyword as there is no instance for this to refer to.
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.
Consider the following piece of code.
public class Question{
Question() {
this();
}
public static void main(String []args) {
Question obj = new Question();
System.out.println(“Java”);
}
}
Which of the following is the output of the above program?
a. Javab. There will be a compile-time error.
c. JavaJava.
d. The program will give a runtime error.
Answer
b. There will be a compile-time error.
If a constructor calls itself, then the error message “recursive constructor invocation” occurs. The
following program is not allowed by the compiler because, inside the constructor, we tried to call
the same constructor. The compiler detects it instantly and throws an error.
Learn Java Programming for free with Computer Course - CompEduBox Android app. Share with your Friends
Consider the following program.
public class Question
{
public static void main(String[] args) {
String str = "Programming in java.";
System.out.println(str.charAt(4)+str.substring(8, 11));
}
}
What is the output of the above program?
a. javab. ring
c. r min
d. gram
Answer
b. ring
Test by a run.
Which of the following statement(s) is/are False?
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 a non-static field.
d. A local variable stores a temporary state; it is declared inside a method.
Answer
c. The term "class variable" is another name for a non-static field.
The term "class variable" is another name for a static field.
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 subclassed by using the final keyword in the class's declaration.
d. An abstract class can only be subclassed; it cannot be instantiated.
Answer
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 subclassed by using the final keyword in the class's
declaration.,
d. An abstract class can only be subclassed; it cannot be instantiated.
All options are correct.
0 Comments