Java MCQ – Classes and Objects
This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Classes and Objects”.
1. What is stored in “obj” in the following line of code?
MyClass obj;
A NULL
B Pointer
C Memory address allocated for the object
D Garbage
2. Which of these keywords is used to create a class?
A class
B struct
C int
D None of the above
3. Which of the following statements is a valid declaration of an object that belongs to “MyClass”?
A MyClass obj = new MyClass();
B MyClass obj = new MyClass;
C obj = new MyClass();
D new MyClass obj;
4. Which of these operators is used to allocate memory for an object?
A malloc
B alloc
C new
D realloc
5. Which of the following statements is incorrect?
A Each class should have a main() method
B The program does not require a main() method
C We can only have one main() method in a program
D main() method must be public
6. What is the output of this program?
class Main { public static void main(String args[]) { int x = 3; if (x == 3) { int x = 4; System.out.println(x); } } }
A Runtime error
B Compilation error
C 3
D 4
7. What is the output of this program?
class MyClass { int width; int height; int length; } public class MainClass { public static void main(String args[]) { MyClass obj = new MyClass(); obj.width = 5; obj.height = 2; obj.length = 5; int y = obj.width * obj.height * obj.length; System.out.print(y); } }
A 10
B 5
C 50
D 25
8. What is the output of this program?
class MyClass { int width; int height; int length; } public class MainClass { public static void main(String args[]) { MyClass objA = new MyClass(); MyClass objB = new MyClass(); objA.height = 1; objA.length = 2; objA.width = 1; objB = objA; System.out.println(objB.height); } }
A Runtime error
B Compilation error
C 1
D 2
9. Which of the following statements is correct?
A Public method is accessible to all other classes in the hierarchy
B Public method is only accessible to subclasses of its parent class
C Public method can only be called by the object of its class
D We can access the public method by calling the object of the public class