Here are 10 essential multiple-choice questions on Java Classes and Objects, covering key concepts.
Question 1
Which of the following is NOT an OOP concept in Java?
Encapsulation
Inheritance
Polymorphism
Compilation
Question 2
What is an object in Java?
A class template
An instance of a class
A function
A primitive data type
Question 3
What is the primary purpose of a constructor in Java?
To allocate memory
To initialize an object
To call methods
To delete objects
Question 5
What will be the output of the following code?
class Test {
int x;
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
0
null
Compilation Error
Runtime Exception
Question 6
What is encapsulation in Java?
Wrapping data and methods into a single unit
Allowing multiple forms of a method
Inheriting properties from a parent class
Overriding methods from an interface
Question 7
What will be the output of the following code?
class Car {
String model;
}
public class Main {
public static void main(String[] args) {
Car c = null;
System.out.println(c.model);
}
}
null
Compilation Error
NullPointerException
Empty String
Question 8
Which of the following is TRUE about constructors?
Constructors must always have a return type
Constructors cannot be overloaded
Constructors can have parameters
A class cannot have multiple constructors
Question 9
What will be the output of the following code?
class Example {
Example() {
System.out.println("Constructor called");
}
}
public class Main {
public static void main(String[] args) {
Example e = new Example();
}
}
Constructor called
No output
Compilation Error
Runtime Error
Question 10
What is the correct way to define a class in Java?
public Example {}
class Example {}
void Example {}
object Example {}
There are 10 questions to complete.