Which keyword is used to make a variable immutable in Java?
Answer: B
'final' keyword makes a variable immutable. Once assigned, its value cannot be changed. 'const' is not a valid Java keyword.
Q.42Easy
Which of the following access modifiers allows a variable to be accessed only within the same class?
Answer: A
The 'private' access modifier restricts access to only within the same class. It provides the highest level of encapsulation.
Q.43Easy
In Java, which keyword is used to create a reference variable that cannot point to a different object after initialization?
Answer: B
The 'final' keyword when applied to a reference variable makes it immutable, meaning it cannot be reassigned to point to another object.
Q.44Easy
What is the primary purpose of using the 'super' keyword in Java?
Answer: B
'super' is used to refer to parent class methods, constructors, and variables. It helps in accessing hidden or overridden members of the parent class.
Q.45Easy
In Java OOP, which principle ensures that internal details of a class are hidden from the outside world?
Answer: C
Encapsulation is the bundling of data and methods into a single unit (class) while hiding internal implementation details using access modifiers.
Advertisement
Q.46Easy
Which of the following correctly describes the relationship between a class and an interface?
Answer: C
In Java, a class implements an interface. A class extends another class. An interface can extend another interface. This is the correct terminology and relationship structure.
Q.47Easy
What will be the output of the following code?
class Parent { void display() { System.out.println("Parent"); } }
class Child extends Parent { void display() { System.out.println("Child"); } }
public class Test { public static void main(String[] args) { Parent p = new Child(); p.display(); } }
Answer: A
This demonstrates runtime polymorphism. Even though p is a Parent reference, it points to a Child object, so the overridden display() method in Child class is executed.
Q.48Easy
What is the primary purpose of the 'super' keyword in Java?
Answer: B
The 'super' keyword is used to refer to parent class methods, constructors, and variables. It allows a child class to access parent class members.
Q.49Easy
Consider the code:
class A { A() { System.out.println("A"); } }
class B extends A { B() { super(); System.out.println("B"); } }
What will be printed when 'new B()' is executed?
Answer: B
The super() call invokes the parent class constructor first, printing 'A', then the child constructor completes, printing 'B'.
Q.50Easy
Which access modifier allows a member to be accessed only within the same class?
Answer: B
The 'private' access modifier restricts access to only the members of the same class. It provides the highest level of encapsulation.
Q.51Easy
Which concept best describes the relationship between Parent and Child classes in 'class Child extends Parent'?
Answer: C
The 'extends' keyword creates an inheritance relationship where Child inherits properties and methods from Parent. This is an 'is-a' relationship.
Q.52Easy
Which of the following correctly describes encapsulation?
Answer: A
Encapsulation is the bundling of data (variables) and methods into a single unit (class) while hiding the internal implementation details from the outside world. It is achieved using access modifiers.
Q.53Easy
What happens when you try to instantiate an interface in Java?
Answer: B
Interfaces cannot be instantiated directly. You must create a concrete class that implements the interface or use an anonymous inner class.
Q.54Easy
Which keyword is used to achieve runtime polymorphism in Java?
Answer: D
Java achieves runtime polymorphism through method overriding using inheritance. Unlike C++, Java doesn't use the 'virtual' keyword; it's implicit for all non-final methods.
Q.55Easy
Can a class extend multiple classes in Java?
Answer: B
Java supports only single class inheritance to avoid the diamond problem. However, multiple interface implementation is allowed.
Q.56Easy
What is the access modifier of members declared in an interface?
Answer: C
All members in an interface are implicitly public. Variables are public static final, and methods are public abstract (unless default or static).
Q.57Easy
What is the default access modifier for class members in Java if not explicitly specified?
Answer: D
If no access modifier is specified for a class member, it has package-private (default) access, meaning it's accessible only within the same package.
Q.58Easy
Which keyword is used to prevent a class from being subclassed?
Answer: B
The 'final' keyword prevents a class from being extended. For example: final class MyClass {} cannot be subclassed.
Q.59Easy
Can you instantiate an interface in Java?
Answer: B
Interfaces cannot be instantiated directly. You can only create objects of classes that implement interfaces.
Q.60Easy
What is the default access modifier for a class member in Java if no modifier is specified?
Answer: D
When no access modifier is specified, the member has package-private (default) access, visible only within the same package.