0% found this document useful (0 votes)
21 views40 pages

OOP Final Exam

The document is a final exam consisting of 120 multiple-choice questions focused on Object-Oriented Programming (OOP) concepts, including basics, advanced topics, and code analysis. It covers key topics such as classes, inheritance, polymorphism, encapsulation, and exception handling in Java. The exam is structured in three sections: basics & core OOP concepts, advanced concepts & code analysis, and mixed conceptual questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views40 pages

OOP Final Exam

The document is a final exam consisting of 120 multiple-choice questions focused on Object-Oriented Programming (OOP) concepts, including basics, advanced topics, and code analysis. It covers key topics such as classes, inheritance, polymorphism, encapsulation, and exception handling in Java. The exam is structured in three sections: basics & core OOP concepts, advanced concepts & code analysis, and mixed conceptual questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

OOP Final Exam – 120 MCQ Questions

1-40: Basics & Core OOP Concepts

1. What keyword is used to create an object from a class?


A) class
B) new
C) object
D) create

2. Which of the following is the correct way to declare a class in Java?


A) class MyClass {}
B) MyClass class {}
C) class = MyClass {}
D) declare class MyClass {}

3. What is an instance variable?


A) A variable declared inside a method
B) A variable shared by all objects
C) A variable declared inside a class but outside any method
D) A constant variable

4. How do you call a method named display() on an object obj?


A) obj.display();
B) display.obj();
C) display(obj);
D) call obj.display();
5. What is the purpose of a constructor?
A) To initialize an object when created
B) To destroy an object
C) To call a method
D) To inherit properties

6. How do you define a constructor for class Car?


A) void Car() {}
B) Car() {}
C) public void Car() {}
D) public Car() {}
7. What does the keyword this refer to inside a class?
A) The class itself
B) The current object
C) A static variable
D) The parent class

8. In which case would you use this keyword?


A) To refer to a local variable
B) To refer to a parameter variable
C) To distinguish between instance variables and parameters with the same
name
D) To declare a static method

9. What keyword is used to inherit a class?


A) implements
B) inherits
C) extends
D) super

10. Which of the following is true about inheritance?


A) Private members are inherited but cannot be accessed directly
B) All members are inherited as public
C) Static members are inherited as instance variables
D) Constructor is inherited by the subclass

11. What keyword is used to call the superclass constructor?


A) this()
B) super()
C) parent()
D) base()

12. If class Dog extends class Animal, which is true?


A) Dog inherits Animal’s private members
B) Dog can override Animal’s methods
C) Dog can access Animal’s private variables directly
D) Dog cannot inherit Animal’s methods

13. What is method overloading?


A) Methods with the same name but different return types
B) Methods with the same name but different parameters
C) Methods overriding a superclass method
D) Using an interface method
14. What is method overriding?
A) Defining a new method with a different name
B) Defining a method in a subclass with the same signature as in the superclass
C) Defining multiple methods with different parameters
D) Defining a static method

15. Which concept allows a superclass reference to refer to a subclass object?


A) Encapsulation
B) Polymorphism
C) Inheritance
D) Abstraction

16. Which of the following is true about abstract classes?


A) They can be instantiated directly
B) They must have at least one abstract method
C) They can have both abstract and concrete methods
D) They do not support inheritance

17. Which keyword is used to declare an interface?


A) interface
B) abstract
C) implements
D) inherits

18. How does a class implement an interface?


A) extends keyword
B) inherits keyword
C) implements keyword
D) uses keyword

19. What access modifier is used to restrict access to class variables?


A) public
B) private
C) protected
D) static

20. What is the purpose of getters and setters?


A) To allow controlled access to private variables
B) To create new variables
C) To delete variables
D) To override methods

21. What keyword is used to handle exceptions?


A) try-catch
B) throw-catch
C) handle-except
D) catch-except

22. Which block will always execute after try-catch?


A) try block
B) catch block
C) finally block
D) throw block
23. Which type of exception must be either caught or declared?
A) unchecked exceptions
B) checked exceptions
C) runtime exceptions
D) logic errors

24. Which class is used to create popup dialogs in Java Swing?


A) JOptionPane
B) JFrame
C) JPanel
D) JLabel

25. What does JOptionPane.showMessageDialog(null, "Hello"); do?


A) Creates a new window titled Hello
B) Displays a popup message box with text "Hello"
C) Logs Hello in the console
D) Opens a dialog for input
26. In Java, a class can implement multiple interfaces.
A) True
B) False

27. Private variables can be accessed directly outside the class.


A) True
B) False

28. Static methods can access instance variables directly.


A) True
B) False

29. Abstract methods must have a method body.


A) True
B) False
30. Constructors have the same name as the class.
A) True
B) False

31. What will be the output of the following code?

java

CopyEdit

class Test {

int x = 5;

void display() {

System.out.println(x);

public class Main {


public static void main(String[] args) {

Test t = new Test();

t.display();

A) 0
B) 5
C) Compilation error
D) Runtime error

32. What is the error in this code?

java

CopyEdit

class Car {

Car() {
System.out.println("Car created");

}
}

public class Main {

public static void main(String[] args) {

Car c = new Car;

A) Missing parentheses while creating object


B) Constructor not defined
C) Syntax error in class declaration
D) No error

33. Match the following terms to their correct definitions:


A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction

1. Wrapping data and methods into a single unit

2. Ability to take many forms

3. Hiding complex details and showing only essentials


4. Acquiring properties from another class

34. The keyword ________ is used to refer to the immediate parent class
constructor.

35. ________ is a mechanism to hide data and restrict access to it.

36. An ________ class cannot be instantiated directly.

37. ________ allows a class to have multiple methods with the same name but
different parameters.

38. Which of the following best describes a final variable?


A) Can be changed after initialization
B) Must be initialized once and cannot change
C) Can be overridden in subclasses
D) Used only in constructors

39. What happens if a subclass does not override a superclass method?


A) Compilation error
B) Inherits the superclass method
C) Method is deleted
D) Object cannot be created

40. What will happen if you try to instantiate an interface?


A) Allowed and creates default methods
B) Compile-time error
C) Runtime error
D) Creates abstract class

41-80: Advanced Concepts & Code Analysis

41. What is the output of this code snippet?

java

CopyEdit

class A {

void show() {

System.out.println("A");

class B extends A {

void show() {

System.out.println("B");
}

}
public class Main {

public static void main(String[] args) {

A obj = new B();

obj.show();

}
A) A
B) B
C) Compilation error
D) Runtime error

42. What access modifier allows a class member to be accessible within the same
package and subclasses?
A) public
B) private
C) protected
D) default

43. What is the output?

java

CopyEdit
class Test {

static int count = 0;

Test() {

count++;

public class Main {

public static void main(String[] args) {

Test t1 = new Test();

Test t2 = new Test();

System.out.println(Test.count);

A) 0
B) 1
C) 2
D) Compilation error
44. Which of the following is NOT a feature of OOP?
A) Polymorphism
B) Inheritance
C) Encapsulation
D) Compilation

45. Which method in Java is used to start a thread?


A) run()
B) start()
C) execute()
D) init()

46. What will happen if you do not override the toString() method in a class?
A) Compilation error
B) Default implementation from Object class is used
C) Runtime error
D) The program crashes

47. What does the keyword final do when applied to a method?


A) Allows overriding
B) Prevents overriding
C) Makes the method static
D) Converts it to an abstract method

48. What is true about interfaces in Java?


A) They can have constructors
B) They can contain implemented methods (default or static)
C) They cannot extend other interfaces
D) They can be instantiated

49. Which is true about the super keyword?


A) Refers to current class
B) Calls a subclass method
C) Refers to the immediate parent class
D) Used to declare new methods

50. Which OOP principle helps to reduce software complexity by hiding unnecessary
details?
A) Encapsulation
B) Polymorphism
C) Inheritance
D) Abstraction
51. What happens if two interfaces have a method with the same signature and a
class implements both?
A) Compilation error
B) The class must override the method once
C) Only one interface’s method is used automatically
D) Runtime error

52. What is the default value of a boolean instance variable in Java?


A) true
B) false
C) null
D) 0

53. What is the output of this code?

java

CopyEdit

class Test {

public static void main(String[] args) {

int x = 10;

int x = 20;

System.out.println(x);

System.out.println(x);
}
}

A) 10 10
B) 20 20
C) Compilation error
D) 20 10

54. What will happen if you declare a class as final?


A) It can be subclassed
B) It cannot be subclassed
C) It can implement interfaces only
D) It cannot have constructors

55. In Java, which of the following is NOT a valid access modifier?


A) public
B) private
C) external
D) protected

56. Which of the following statements about static variables is true?


A) They belong to the instance of the class
B) They belong to the class itself
C) They can be overridden
D) They must be initialized in the constructor

57. What is encapsulation?


A) Hiding the internal state and requiring all interaction through methods
B) Using multiple methods with the same name
C) Deriving new classes from existing ones
D) Allowing a superclass reference to point to subclass objects

58. Which of the following can be used to implement polymorphism in Java?


A) Method overloading
B) Method overriding
C) Interfaces
D) All of the above

59. Can constructors be overloaded?


A) Yes
B) No
60. Which of the following are not inherited by a subclass?
A) public members
B) protected members
C) private members
D) default members

81-120: Mixed Conceptual, Code Output & True/False

61. True or False: An interface can have instance variables.


A) True
B) False
62. True or False: An abstract class can have constructors.
A) True
B) False

63. True or False: Static methods can be overridden in Java.


A) True
B) False

64. True or False: Java supports multiple inheritance using classes.


A) True
B) False

65. What is the output?

java

CopyEdit

class Test {

int x = 5;

void setX(int x) {

this.x = x;

void display() {

System.out.println(x);

}
public class Main {

public static void main(String[] args) {


Test t = new Test();

t.setX(10);

t.display();

}
A) 5
B) 10
C) Compilation error
D) Runtime error

66. What happens if a class does not implement all methods of an interface?
A) The class must be declared abstract
B) Compilation error
C) Methods are auto-implemented
D) Runtime error

67. What keyword is used to throw an exception manually?


A) throw
B) throws
C) try
D) catch

68. What is the purpose of the finally block?


A) To catch exceptions
B) To execute code regardless of exceptions
C) To declare exceptions
D) To throw exceptions

69. Which of these is NOT a valid method signature in Java?


A) public int sum(int a, int b)
B) private void display()
C) public static final int compute()
D) void 123method()

70. What is the output?


java

CopyEdit

public class Test {

static int count = 0;

Test() { count++; }

public static void main(String[] args) {

Test t1 = new Test();


Test t2 = new Test();
Test t3 = new Test();

System.out.println(count);

A) 0
B) 1
C) 2
D) 3
71. What does polymorphism allow in OOP?
A) Multiple inheritance
B) Overloading constructors only
C) Using a single interface to represent different types
D) Hiding data

72. Can abstract classes have implemented methods?


A) Yes
B) No

73. Which statement is true about the super keyword?


A) It calls a subclass constructor
B) It can be used to access superclass variables and methods
C) It is used to create new objects
D) It cannot be used in constructors

74. What is the output?

java
CopyEdit

class A {

void method() { System.out.println("A"); }

class B extends A {

void method() { System.out.println("B"); }

class C extends B {}
public class Main {

public static void main(String[] args) {

A obj = new C();

obj.method();

A) A
B) B
C) C
D) Compilation error

75. True or False: You can declare a method both abstract and final.
A) True
B) False
76. True or False: Interfaces can extend other interfaces.
A) True
B) False

77. Which of these is NOT a characteristic of an abstract class?


A) Can have abstract methods
B) Can have constructors
C) Can be instantiated directly
D) Can have concrete methods

78. What does encapsulation promote?


A) Data hiding
B) Multiple inheritance
C) Method overloading
D) Polymorphism

79. What keyword is used to prevent a class from being subclassed?


A) static
B) final
C) abstract
D) private

80. What will be the output?


java
CopyEdit

class Test {

static int x = 10;

void method() {

System.out.println(x);

public class Main {


public static void main(String[] args) {

Test t = new Test();

t.method();

A) 0
B) 10
C) Compilation error
D) Runtime error

81-120: Mixed Conceptual, Code Output & True/False (continued)

81. Which of the following is the correct way to declare an array of integers in Java?
A) int[] arr;
B) int arr[];
C) Both A and B
D) array int arr;

82. What will happen if you try to access an array index out of bounds in Java?
A) Compilation error
B) ArrayIndexOutOfBoundsException at runtime
C) Returns null
D) Returns zero

83. What is encapsulation in OOP?


A) Hiding data and providing public methods for access
B) Deriving new classes from existing ones
C) Ability of methods to take many forms
D) Creating multiple methods with the same name

84. Which method signature correctly overrides the equals() method from the Object
class?
A) public boolean equals(Object obj)
B) public boolean equals(Test obj)
C) private boolean equals(Object obj)
D) public void equals(Object obj)

85. What does the static keyword mean for a method or variable?
A) It belongs to the class, not instances
B) It can only be accessed inside the class
C) It is private by default
D) It is final

86. What is the output of this code?

java

CopyEdit

class Test {

static int a = 5;

void increment() { a++; }

public class Main {

public static void main(String[] args) {

Test t1 = new Test();

Test t2 = new Test();

t1.increment();

t2.increment();
System.out.println(Test.a);

}
A) 5
B) 6
C) 7
D) 8

87. What type of polymorphism is achieved by method overloading?


A) Compile-time polymorphism
B) Runtime polymorphism
C) Data polymorphism
D) None of the above

88. What type of polymorphism is achieved by method overriding?


A) Compile-time polymorphism
B) Runtime polymorphism
C) Data polymorphism
D) None of the above

89. Which of the following keywords is used to declare a constant?


A) constant
B) final
C) static final
D) Both B and C

90. What will happen if you declare a method as private and try to override it in a
subclass?
A) Compiles and overrides
B) Compilation error
C) The subclass method is treated as a new method
D) Runtime error

91. Which of the following can NOT be declared inside an interface?


A) Abstract methods
B) Static methods
C) Instance variables
D) Default methods

92. Which access modifier allows members to be accessed only within the same
class?
A) public
B) private
C) protected
D) default
93. What does the abstract keyword do when applied to a method?
A) Provides a default implementation
B) Declares the method without a body
C) Makes the method static
D) Makes the method final

94. Can a subclass constructor call a superclass constructor?


A) Yes, using super()
B) No
C) Only if both have the same name
D) Only for abstract classes

95. What will be the output?

java

CopyEdit
class Parent {

void show() { System.out.println("Parent"); }

class Child extends Parent {

void show() { System.out.println("Child"); }

public class Test {

public static void main(String[] args) {

Parent p = new Parent();

Parent c = new Child();

p.show();

c.show();

}
A) Parent Parent
B) Child Child
C) Parent Child
D) Child Parent
96. Which statement about interfaces is true?
A) They can have instance variables
B) They can have constructors
C) They can have default and static methods
D) They cannot extend other interfaces

97. True or False: The super keyword can be used to access superclass variables
and methods.
A) True
B) False

98. What happens when you try to instantiate an abstract class?


A) Allowed
B) Compile-time error
C) Runtime exception
D) The abstract class is automatically instantiated

99. Which of the following is NOT true about encapsulation?


A) It helps protect data
B) It bundles data with methods
C) It makes data publicly accessible
D) It hides internal object details

100. Which of these is NOT a valid Java identifier?


A) _myVar
B) $value
C) 9value
D) myValue9

101. What is the default return type of the main method in Java?
A) void
B) int
C) String
D) No return type

102. What will happen if a class implements an interface but does not
implement all of its methods?
A) Compilation error
B) Runtime error
C) The class is abstract by default
D) Methods are auto-implemented

103. Which of the following statements is correct about the final keyword?
A) Can be used with variables, methods, and classes
B) Makes variables modifiable
C) Allows methods to be overridden
D) Allows classes to be subclassed

104. What exception is thrown when you try to access a null object reference?
A) NullPointerException
B) IOException
C) ArrayIndexOutOfBoundsException
D) ClassCastException
105. Which of these keywords is used to declare a package?
A) package
B) import
C) include
D) module

106. What does the implements keyword do?


A) Defines a subclass
B) Indicates a class is implementing an interface
C) Declares a package
D) Imports a class

107. Can constructors be inherited?


A) Yes
B) No

108. What is the output?

java

CopyEdit
class A {

void print() { System.out.println("A"); }

class B extends A {

void print() { System.out.println("B"); }

class C extends B {

void print() { System.out.println("C"); }


}

public class Test {

public static void main(String[] args) {

A obj = new C();

obj.print();

A) A
B) B
C) C
D) Compilation error

109. What will happen if you do not implement all abstract methods of an
abstract class in its subclass?
A) Compile-time error
B) Runtime error
C) The subclass becomes abstract automatically
D) The program crashes

110. Which statement about static blocks is true?


A) They run once when the class is loaded
B) They run every time an object is created
C) They are used to declare instance variables
D) They are similar to constructors

111. What is the purpose of the finally block in exception handling?


A) To catch exceptions
B) To execute code regardless of exceptions
C) To declare exceptions
D) To throw exceptions

112. What is the output?

java

CopyEdit

public class Test {

public static void main(String[] args) {


try {

int x = 5 / 0;

} catch (ArithmeticException e) {

System.out.println("Error");

} finally {

System.out.println("Finally block");

}
}

A) Error
B) Finally block
C) Error
Finally block
D) Compilation error

113. Which of the following are true about Java packages?


A) Used to organize classes
B) Prevent naming conflicts
C) Allow access control
D) All of the above

114. What does the super() call do?


A) Calls current class constructor
B) Calls superclass constructor
C) Calls a static method
D) Calls a private method

115. What happens if a static method tries to access an instance variable?


A) Works normally
B) Compile-time error
C) Runtime error
D) Accesses default value

116. Which of these methods cannot be overridden?


A) Static methods
B) Instance methods
C) Abstract methods
D) Final methods
117. Which of the following is NOT an OOP principle?
A) Abstraction
B) Encapsulation
C) Compilation
D) Polymorphism

118. What keyword is used to declare a variable that cannot be changed?


A) static
B) final
C) const
D) immutable

119. Which method is used to get the runtime class of an object?


A) getClass()
B) classOf()
C) getRuntime()
D) type()

120. What is the output of this code?

java

CopyEdit

public class Test {

public static void main(String[] args) {

String s1 = "hello";

String s2 = "hello";

System.out.println(s1 == s2);
}

A) true
B) false
C) Compilation error
D) Runtime error

1. What does OOP stand for?


A) Object-Oriented Programming
B) Open-Ordered Process
C) Overloaded Operations Procedure
D) Operating Object Protocol
2. Which of the following is NOT a pillar of OOP?
A) Inheritance
B) Polymorphism
C) Encapsulation
D) Compilation
3. What is encapsulation?
A) Hiding data inside classes and providing access via methods
B) Creating many methods with the same name
C) Deriving new classes from existing classes
D) A feature that allows one interface to represent many types
4. Which keyword is used to inherit a class in Java?
A) implement
B) extends
C) inherits
D) super
5. Which of the following is true about constructors?
A) Constructors have a return type
B) Constructors can be overloaded
C) Constructors cannot have parameters
D) Constructors are static methods
6. What does the this keyword refer to?
A) Current method
B) Current class variable
C) Current object
D) Parent class
7. What is method overloading?
A) Same method name with different parameters in the same class
B) Replacing a method in subclass with a new version
C) Using multiple methods with different names
D) Creating methods with the same return type
8. What is method overriding?
A) Same method name and parameters in subclass to replace superclass method
B) Different methods with same name in the same class
C) Methods with the same name but different parameters
D) A static method in the subclass
9. Which of the following access modifiers is the most restrictive?
A) public
B) protected
C) private
D) default
10. What is polymorphism?
A) The ability of objects to take many forms
B) The process of inheriting classes
C) Encapsulating data and methods
D) Writing multiple constructors
11. Can an abstract class be instantiated?
A) Yes
B) No
12. What keyword declares a class as abstract?
A) final
B) abstract
C) static
D) void
13. What is the purpose of the super keyword?
A) To call the superclass constructor or methods
B) To define a new class
C) To access private members
D) To create a new object
14. Which of the following can NOT be done with an interface?
A) Have abstract methods
B) Have implemented methods (default or static)
C) Instantiate directly
D) Extend other interfaces
15. What is the output of this code?

class Test {

int x = 5;

void display() { System.out.println(x); }


public static void main(String[] args) {

Test t = new Test();


t.display();

A) 0
B) 5
C) Compilation error
D) Runtime error
16. Which exception is checked at compile time?
A) NullPointerException
B) IOException
C) ArithmeticException
D) RuntimeException
17. What happens if you don’t handle a checked exception?
A) Compile-time error
B) Runtime error
C) Warning only
D) Nothing
18. What is the default value of an int instance variable in Java?
A) 0
B) null
C) Undefined
D) 1
19. Can static methods be overridden?
A) Yes
B) No
20. What keyword is used to define a constant in Java?
A) const
B) static
C) final
D) constant

21. What is the difference between == and .equals() in Java?


A) == compares references, .equals() compares values
B) == compares values, .equals() compares references
C) Both compare references
D) Both compare values
22. Which of these is NOT true about inheritance?
A) Subclass inherits public and protected members of superclass
B) Subclass inherits private members of superclass
C) Subclass can add new members
D) Subclass can override superclass methods
23. What happens if you declare a method as final?
A) It can be overridden
B) It cannot be overridden
C) It becomes static
D) It becomes abstract
24. What is an interface in Java?
A) A class with implemented methods only
B) A blueprint for classes with abstract methods
C) A class that cannot be inherited
D) A package of methods
25. What is the default access modifier if none is specified?
A) private
B) public
C) protected
D) package-private (default)
26. Which of these is NOT a valid identifier in Java?
A) _myVar
B) $value
C) 9value
D) myValue9
27. What is the output?

public class Test {

public static void main(String[] args) {

int x = 10;

int x = 20;

System.out.println(x);
}

A) 10
B) 20
C) Compilation error
D) Runtime error
28. What keyword is used to handle exceptions?
A) try
B) catch
C) finally
D) All of the above
29. What is the use of the finally block?
A) To catch exceptions
B) To execute code after try-catch regardless of exception
C) To throw exceptions
D) To declare exceptions
30. What will happen if a subclass does not implement all abstract methods of its
abstract superclass?
A) Compile-time error
B) Runtime error
C) Subclass becomes abstract automatically
D) The program compiles fine
31. What does static mean for variables and methods?
A) They belong to the class, not instances
B) They belong to each instance
C) They are private
D) They are final
32. Can a static method access instance variables directly?
A) Yes
B) No
33. What is the output?

public class Test {

static int count = 0;


public Test() { count++; }

public static void main(String[] args) {

Test t1 = new Test();

Test t2 = new Test();

System.out.println(count);

A) 0
B) 1
C) 2
D) Compilation error
34. What is method overriding used for?
A) To provide specific implementation in subclass
B) To overload methods
C) To define multiple constructors
D) To create static methods
35. Which of the following is NOT true about constructors?
A) They have the same name as the class
B) They have no return type
C) They can be inherited
D) They initialize objects
36. What is a package in Java?
A) A collection of classes and interfaces
B) A variable type
C) A method type
D) A class constructor
37. What is the purpose of the import statement?
A) To include packages or classes in your program
B) To create new classes
C) To declare variables
D) To handle exceptions
38. What will happen if you declare a class as final?
A) It can be subclassed
B) It cannot be subclassed
C) It must have abstract methods
D) It cannot have methods
39. Can interface methods have implementations?
A) Yes, default and static methods can
B) No, all methods must be abstract
C) Yes, all methods can have implementations
D) Only private methods can have implementations
40. What does the instanceof keyword do?
A) Checks if an object is an instance of a class or subclass
B) Casts an object to a class
C) Creates an instance of a class
D) Deletes an object
41. Which of these exceptions is unchecked?
A) IOException
B) NullPointerException
C) FileNotFoundException
D) SQLException
42. What is the superclass of all classes in Java?
A) Object
B) Class
C) String
D) Main
43. What is the output?

public class Test {

public static void main(String[] args) {

String s1 = new String("hello");

String s2 = new String("hello");


System.out.println(s1 == s2);

A) true
B) false
C) Compilation error
D) Runtime error
44. Which of these can be used to document Java code?
A) /* comment /
B) // comment
C) /* Javadoc comment */
D) All of the above
45. What is the output?

public class Test {

public static void main(String[] args) {

int[] arr = {1, 2, 3};

System.out.println(arr.length);

A) 3
B) 2
C) Compilation error
D) Runtime error
46. What does throws keyword do?
A) Catches exceptions
B) Declares exceptions a method might throw
C) Throws exceptions
D) Handles exceptions

‫ بنفس التنسيق‬100 ‫ لـ‬61 ‫ نكمل مع بعض من السؤال‬،‫تمام يا أحمد‬:

61. What will happen if you try to compile the following?

public class Test {

public static void main(String[] args) {

int x;
System.out.println(x);

}
A) Prints 0
B) Compilation error (variable x might not have been initialized)
C) Runtime error
D) Prints garbage value
62. What is the effect of the break statement inside a loop?
A) Skips current iteration
B) Exits the loop immediately
C) Continues to the next iteration
D) Causes an error
63. Which of these is NOT a Java primitive type?
A) int
B) boolean
C) String
D) double
64. Which of the following is true about ArrayList?
A) Fixed size array
B) Resizable array implementation
C) Only stores primitive types
D) Cannot store objects
65. What is the difference between throw and throws?
A) throw declares an exception, throws throws it
B) throw throws an exception, throws declares it
C) Both throw exceptions
D) Both declare exceptions
66. What is the output?

public class Test {

public static void main(String[] args) {

int[] arr = new int[3];

System.out.println(arr[0]);

A) 0
B) null
C) Compilation error
D) Runtime error
67. Which of the following statements about garbage collection in Java is true?
A) It is manual and controlled by the programmer
B) It is automatic and runs periodically
C) It deletes all objects instantly
D) It is not supported in Java
68. What keyword is used to inherit from a superclass?
A) super
B) inherit
C) extends
D) implements
69. Which of the following is true about private members?
A) Accessible in all classes
B) Accessible only within the declaring class
C) Accessible in the same package
D) Accessible in subclasses only
70. What is the output?

public class Test {

public static void main(String[] args) {

String str = "Hello";

str = str.toUpperCase();

System.out.println(str);
}

A) hello
B) HELLO
C) Compilation error
D) Runtime error
71. Which of these is true about abstract methods?
A) They have a body
B) They do not have a body
C) They can be static
D) They can be private
72. What happens if a class implements multiple interfaces with the same method
signature?
A) Compiler error
B) Class must override the method
C) Uses method from first interface only
D) Uses method from last interface only
73. What does the keyword static mean when applied to a block?
A) The block executes once when class is loaded
B) The block executes every time an instance is created
C) The block cannot be static
D) The block executes on method call
74. Which of these is NOT a valid Java identifier?
A) _myVar
B) 2ndVar
C) $value
D) var_123
75. What is polymorphism in Java?
A) Multiple inheritance of classes
B) Ability of an object to take many forms
C) Creating many objects from one class
D) Writing many methods in a class
76. Which method is called when an object is created?
A) main()
B) finalize()
C) constructor
D) static block
77. Which of the following is true about final variables?
A) Can be reassigned
B) Cannot be reassigned once initialized
C) Are static by default
D) Can be declared inside methods only
78. What will happen if you call a method on a null reference?
A) NullPointerException at runtime
B) Compilation error
C) Method executes normally
D) Returns null
79. What is the correct way to declare a main method in Java?
A) public static void main(String[] args)
B) static public main(String[] args)
C) void main(String args)
D) public void main(String[] args)
80. What is the difference between abstract class and interface?
A) Abstract classes can have implemented methods, interfaces cannot
B) Interfaces can have implemented methods, abstract classes cannot
C) Abstract classes cannot have constructors
D) Interfaces can be instantiated
81. Which of the following is true about the this keyword?
A) Refers to the current object
B) Refers to the superclass
C) Refers to a static variable
D) Refers to the main method
82. What is method signature?
A) Method name and return type
B) Method name and parameters list
C) Method name only
D) Parameters list only
83. What will be the output?

public class Test {

public static void main(String[] args) {

int a = 5;
int b = ++a + a++;

System.out.println(b);

A) 11
B) 12
C) 10
D) 9
84. What keyword is used to prevent inheritance?
A) static
B) final
C) abstract
D) private
85. Which of these access modifiers allows access only within the same
package?
A) public
B) private
C) protected
D) default (package-private)
86. What will happen if you try to override a static method?
A) It is allowed and works as overriding
B) It causes a compile-time error
C) It hides the method, not overrides it
D) It makes method abstract
87. What is the output?

public class Test {

public static void main(String[] args) {

String s = null;

System.out.println(s + " test");

A) null test
B) Compilation error
C) Runtime error
D) test null
88. Which of these is NOT true about super keyword?
A) Can be used to call superclass constructor
B) Can be used to call superclass method
C) Can be used to access private members of superclass
D) Can be used to access immediate parent class members
89. What is the purpose of the instanceof operator?
A) To create new instance
B) To check object type
C) To cast objects
D) To delete objects
90. What is the result of this code?

int x = 10;

int y = x > 5 ? 1 : 0;

System.out.println(y);

A) 10
B) 1
C) 0
D) Compilation error
91. What is the default value of a boolean instance variable?
A) true
B) false
C) null
D) 0
92. Can constructors be overloaded?
A) Yes
B) No
93. What is the output?

public class Test {

public static void main(String[] args) {

int i = 0;
while (i < 3) {

System.out.print(i + " ");

i++;

A) 0 1 2
B) 0 1 2 3
C) 1 2 3
D) Compilation error
94. What is the output?

public class Test {

public static void main(String[] args) {

String s1 = "abc";

String s2 = "abc";

System.out.println(s1 == s2);

A) true
B) false
C) Compilation error
D) Runtime error
95. What is the main advantage of using interfaces?
A) To support multiple inheritance
B) To allow private methods
C) To create objects directly
D) To override constructors
96. Which statement is true about try-catch?
A) try block contains code that might throw exceptions
B) catch block handles exceptions
C) Both A and B
D) Neither A nor B
97. Which is true about static blocks?
A) They execute once when the class is loaded
B) They execute every time an instance is created
C) They can throw checked exceptions
D) They are optional
98. What is the output?

public class Test {

public static void main(String[] args) {


int a = 5;

int b = 2;

System.out.println(a / b);

A) 2
B) 2.5
C) Compilation error
D) Runtime error
99. What does encapsulation protect against?
A) Direct access to object data
B) Inheritance
C) Polymorphism
D) Abstraction
100. Which of the following is true about final classes?
A) They can be subclassed
B) They cannot be subclassed
C) They must have abstract methods
D) They can implement interfaces

You might also like