0% found this document useful (0 votes)
9 views

java choice

The document contains a set of 100 multiple-choice questions (MCQs) focused on Object-Oriented Programming (OOP) concepts in Java, covering topics such as encapsulation, inheritance, polymorphism, and interfaces. Each question is accompanied by four answer options and the correct answer is provided. This resource serves as a study guide for individuals looking to test or enhance their understanding of OOP principles in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

java choice

The document contains a set of 100 multiple-choice questions (MCQs) focused on Object-Oriented Programming (OOP) concepts in Java, covering topics such as encapsulation, inheritance, polymorphism, and interfaces. Each question is accompanied by four answer options and the correct answer is provided. This resource serves as a study guide for individuals looking to test or enhance their understanding of OOP principles in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Sure!

Here’s a set of 100 multiple-choice questions (MCQs) related to Object-


Oriented Programming (OOP) concepts, particularly in Java. Each question is
followed by four options and the correct answer.

▎Object-Oriented Programming MCQs

1. What does OOP stand for?

• A) Object Oriented Programming

• B) Object Organized Programming

• C) Object Oriented Protocol

• D) Object Oriented Process


Answer: A

2. Which of the following is not a feature of OOP?

• A) Encapsulation

• B) Inheritance

• C) Polymorphism

• D) Compilation
Answer: D

3. What is encapsulation?

• A) Hiding the implementation details

• B) Sharing code between classes

• C) Creating a new class from an existing one


• D) Using a single interface for different data types
Answer: A

4. Which keyword is used to create a class in Java?

• A) new

• B) class

• C) create

• D) object
Answer: B

5. What is inheritance in OOP?

• A) The ability to create multiple classes from one class

• B) The ability to create subclasses from a parent class

• C) The ability to hide data

• D) The ability to overload methods


Answer: B

6. Which of the following is an example of polymorphism?

• A) Method Overloading

• B) Method Overriding

• C) Both A and B

• D) None of the above


Answer: C

7. What is the purpose of the 'super' keyword in Java?

• A) To call a method from a subclass

• B) To call a method from a superclass

• C) To create a new object

• D) To define a new class


Answer: B

8. Which of the following access modifiers allows access only within the same
package?

• A) private

• B) protected

• C) public

• D) default
Answer: D

9. What does the term 'abstraction' mean in OOP?

• A) Hiding complex implementation details and showing only essential features

• B) Creating new classes from existing ones

• C) Using multiple methods with the same name

• D) Grouping related properties and behaviors


Answer: A
10. Which of the following is used to implement abstraction in Java?

• A) Abstract Classes

• B) Interfaces

• C) Both A and B

• D) None of the above


Answer: C

11. What is an interface in Java?

• A) A class with no implementation

• B) A class that can be instantiated

• C) A collection of methods with implementations

• D) An abstract class with some methods implemented


Answer: A

12. Which of the following statements is true about constructors in Java?

• A) Constructors have a return type.

• B) Constructors can be overloaded.

• C) Constructors can be private.

• D) All of the above.


Answer: B

13. What is method overloading?


• A) Using the same method name with different parameters

• B) Using the same method name in different classes

• C) Changing the return type of a method

• D) None of the above


Answer: A

14. Which keyword is used to define an abstract method in Java?

• A) abstract

• B) void

• C) interface

• D) new
Answer: A

15. What will happen if you try to instantiate an abstract class?

• A) It will compile and run successfully.

• B) It will compile but throw an error at runtime.

• C) It will not compile.

• D) None of the above.


Answer: C

16. Which of the following allows a class to inherit fields and methods from
another class?
• A) Interface

• B) Abstract Class

• C) Inheritance

• D) Encapsulation
Answer: C

17. What is the default value of an instance variable in Java?

• A) 0 for numeric types, false for boolean, null for objects

• B) 0 for all types

• C) null for all types

• D) Depends on the data type


Answer: A

18. Which feature allows you to define multiple methods with the same name but
different parameters?

ChatGPT 4 | Midjourney | Claude | Suno, [12/9/2024 10:43 PM]


• A) Polymorphism

• B) Encapsulation

• C) Inheritance

• D) Method Overloading
Answer: D

19. What does 'this' keyword refer to in Java?


• A) The current object instance

• B) The parent class instance

• C) The static method context

• D) None of the above


Answer: A

20. Which of the following is NOT a principle of OOP?

• A) Encapsulation

• B) Inheritance

• C) Compilation

• D) Polymorphism
Answer: C

21. What is the primary benefit of using interfaces?

• A) They allow multiple inheritance.

• B) They provide method implementations.

• C) They allow code to be reused across different classes.

• D) They are faster than classes.


Answer: C

22. In Java, which keyword is used to indicate that a class cannot be subclassed?

• A) final
• B) static

• C) private

• D) abstract
Answer: A

23. What will happen if you do not provide any constructor in your class?

• A) The compiler will not allow it.

• B) A default constructor will be provided by the compiler.

• C) The class cannot be instantiated.

• D) None of the above.


Answer: B

24. What is the main purpose of using 'protected' access modifier?

• A) Allows access only within the same package.

• B) Allows access to subclasses and classes in the same package.

• C) Allows access to all classes.

• D) None of the above.


Answer: B

25. Which of the following allows you to implement multiple inheritance in Java?

• A) Classes

• B) Interfaces
• C) Abstract Classes

• D) None of the above


Answer: B

26. What does 'final' keyword signify when applied to a variable?

• A) The variable can be changed later.

• B) The variable cannot be changed once initialized.

• C) The variable can only be accessed within its own class.

• D) None of the above.


Answer: B

27. What is a constructor chaining in Java?

• A) Calling one constructor from another within the same class.

• B) Creating multiple constructors with different parameters.

• C) Using super() to call parent class constructors.

• D) All of the above.


Answer: D

28. Which of these is true about method overriding?

• A) It can change the return type.

• B) It must have the same name and parameters as the method in superclass.

• C) It can only occur in abstract classes.


• D) None of the above.
Answer: B

29. What happens if a subclass does not implement an abstract method from its
superclass?

• A) It will compile successfully.

• B) It must also be declared as abstract.

• C) It will throw an error at runtime.

• D) None of the above.


Answer: B

30. In Java, which keyword is used to prevent method overriding?

• A) final

• B) static

• C) private

• D) protected
Answer: A

31. Which of these statements about interfaces is true?

• A) Interfaces can have constructors.

• B) Interfaces can contain instance variables.

• C) Interfaces can extend multiple interfaces.

• D) Interfaces can have static methods only.


Answer: C

32. In which scenario would you use composition over inheritance?

• A) When you need shared behavior across classes.

• B) When you want to express an "is-a" relationship.

• C) When you want to express a "has-a" relationship.

• D) None of the above.


Answer: C

33. What is a package in Java?

• A) A collection of related classes and interfaces.

• B) A single class definition.

• C) An abstract representation of an object.

• D) None of the above.


Answer: A

34. What keyword is used to inherit from a superclass in Java?

• A) inherit

• B) extends

• C) implements

• D) super
Answer: B
35. Which method can be used to convert an object into a string representation in
Java?

• A) print()

• B) display()

• C) toString()

• D) valueOf()
Answer: C

36. What does it mean if a class is marked as 'abstract'?

• A) It cannot have any methods.

• B) It cannot be instantiated directly.

• C) It must implement all its methods.

• D) None of the above.


Answer: B

37. Which keyword is used to refer to the superclass's constructor in Java?

• A) this

• B) super

• C) parent

• D) base
Answer: B

38. What does 'method overriding' allow you to do?


• A) Change method names in subclasses.

• B) Provide specific implementation for methods defined in superclass.

• C) Use different parameter types in subclasses.

• D) Change access modifiers in subclasses.


Answer: B

39. Which statement about polymorphism is false?

• A)
Polymorphism allows methods to do different things based on the object it is
acting upon.

• B)
It can be achieved through method overloading and overriding.

• C)
Polymorphism can only occur with inherited methods.

• D)
Polymorphism enhances flexibility and maintainability in code design.
Answer: C

40. What does 'dynamic binding' mean in OOP?

• A)
The type of object is determined at compile time.

• B)
The type of object is determined at runtime.

• C)
The type of method is fixed at compile time.

• D)
None of the above.
Answer: B

41. Which type of relationship does "is-a" represent in OOP?

• A)
Composition

• B)
Aggregation

• C)
Inheritance

• D)
Encapsulation
Answer: C

42. In Java, what does 'static' mean when applied to variables or methods?

• A)
They belong to instances of a class rather than the class itself.

• B)
They are shared among all instances of a class.

• C)
They cannot be accessed outside their own class.

• D)
They can only be accessed by subclasses.
Answer: B
43. Which feature of OOP allows for defining a common interface for different
data types?

• A)
Encapsulation

• B)
Abstraction

• C)
Inheritance

• D)
Polymorphism
Answer: B

44. What does it mean if a method or variable is declared as 'private'?

• A)
It can be accessed from anywhere within the program.

• B)
It can only be accessed within its own class.

• C)
It can be accessed by subclasses only.

• D)
It can be accessed by any other classes in the same package.
Answer: B

45. Which statement about constructors is true?

• A)
A constructor must have a return type.

• B)
A constructor name must match its class name exactly, including case sensitivity.

• C)
Constructors can be static methods.

• D)
Constructors cannot be overloaded.
Answer: B

46. What happens if you declare a variable as 'final'?

• A)
The variable can never be changed after initialization.

• B)
The variable can only be changed once during its lifetime.

• C)
The variable can be changed but not reassigned entirely.

• D)
None of these statements are correct.
Answer: A

47. Which type of inheritance allows a subclass to inherit from multiple


superclasses?

• A)
Single Inheritance

• B)
Multiple Inheritance
• C)
Multilevel Inheritance

• D)
Java does not support multiple inheritance through classes; it supports it through
interfaces only.
Answer: D

48. What will happen if you try to access a private member from outside its class?

• A)
It will compile successfully but throw an error at runtime.

• B)
It will not compile; an access error will occur at compile time.

• C)
It will compile but give incorrect results at runtime.

• D)
There are no restrictions on accessing private members; they are public by default.

Answer: B

49. Which statement about aggregation is true?

• A)
It represents a strong relationship between objects where one cannot exist without
another.

• B)
It represents a weak relationship where one object contains another but both can
exist independently.
• C)
It is another term for inheritance in OOP design.

• D)
None of these statements are correct.
Answer: B

50. Which statement about interfaces and abstract classes is true?

• A)
An interface cannot contain any methods while an abstract class can have both
abstract and concrete methods.

• B)
An abstract class cannot implement interfaces while interfaces can implement
abstract classes.

• C)
Both interfaces and abstract classes can have method implementations since Java
8.

• D)
Interfaces can have constructors while abstract classes cannot have constructors at
all.
Answer: C

51. What does 'interface segregation principle' refer to?

• A)
Clients should not be forced to depend on methods they do not use; hence, many
client-specific interfaces are better than one general-purpose interface.

• B)
Interfaces should contain all methods required by clients regardless of their usage
frequency or relevance.
• C)
Interfaces should always inherit from other interfaces, ensuring that all
functionality remains intact across implementations.

• D)
None of these statements are correct.
Answer: A

52. Which statement about inner classes is true?

• A)
Inner classes cannot access members (including private ones).

• B)
Inner classes are declared outside any other class and cannot access outer class
members directly.

• C)
Inner classes can access members (including private ones).

• D)
Inner classes must always be static and cannot have non-static members
themselves.
Answer: C

53. Which method allows you to compare two objects for equality in Java?

• A)
compareTo()

• B)
equals()

• C)
== operator

• D)
compare()
Answer: B

54. What happens when you call 'super()' inside a constructor?

• A)
It calls another constructor from within that same constructor's class context,
providing additional initialization logic before executing its body.

• B)
It calls the superclass's constructor before executing any code inside that
constructor's body, allowing proper initialization order between parent and child
classes' attributes or behaviors as needed based on constructor parameters passed
along if applicable too!

• C)
It throws an error since constructors cannot call super().

• D)
None of these statements are correct!
Answer: B

55. What does it mean if an interface extends another interface?

• A)
The new interface inherits all methods from its parent interface, thereby allowing
any implementing class access as well!

• B)
The new interface overrides existing methods from its parent interface while
adding new ones simultaneously!
• C)
The new interface must provide implementations for all inherited methods
immediately upon declaration!

• D)
None of these statements are correct!
Answer: A

56. How do you achieve encapsulation in Java?

• A)
By declaring variables as public so they are accessible anywhere!

• B)
By using private variables along with public getter/setter methods!

• C)
By using static methods only!

• D)
None of these statements are correct!
Answer: B

57. Which concept allows different classes to provide different implementations


for methods defined by an interface?

• A)
Abstraction

• B)
Encapsulation

• C)
Polymorphism
• D)
Inheritance
Answer: C

58. What will happen if you try to override a static method?

• A)
It will compile successfully but behave differently at runtime depending on which
reference type invokes it!

• B)

It will throw an error during compilation since static methods cannot be


overridden!

• C)
It will override without any issues since static binding occurs at compile time
instead!

• D)
None of these statements are correct!
Answer: C

59. What does 'composition' imply regarding object relationships?



A)
One object can contain references to other objects, establishing ownership while
allowing independent lifetimes!

B)
One object inherits behavior from another object without retaining ownership!

C)
Two unrelated objects interact without influencing each other's lifecycle!

D)
None of these statements are correct!
Answer: A

60. Which statement about exception handling in OOP is true?



A)
Exceptions should always terminate program execution immediately!

B)
Exception handling mechanisms like try-catch blocks enhance robustness by
preventing unexpected crashes during runtime!

C)
All exceptions must be handled explicitly via checked exceptions!

D)
None of these statements are correct!
Answer: B

61. What happens if you declare a method as 'synchronized'?



A)
Only one thread can execute that method at any given time, ensuring thread safety!

B)
All threads must wait indefinitely until that method finishes execution regardless!

C)
Multiple threads may still execute concurrently without restrictions!

D)
None of these statements are correct!
Answer: A
62. Which statement about garbage collection in Java is true?

A)
Garbage collection occurs automatically without requiring explicit memory
management!

B)
Garbage collection must always be triggered manually by invoking System.gc()!

C)
Garbage collection guarantees immediate reclamation upon object dereferencing!

D)
None of these statements are correct!
Answer: A

63. What does 'method hiding' refer to?



A)
When two methods share identical names across different scopes!

B)
When subclasses override static methods defined in their parent classes!

C)
When instance variables become inaccessible due to conflicting names within
nested scopes!

D)
None of these statements are correct!
Answer: B

64. Which statement regarding anonymous inner classes is true?



A)
Anonymous inner classes cannot extend other classes or implement interfaces
simultaneously!

B)
Anonymous inner classes provide concise syntax for instantiating single-use
implementations without naming them explicitly!

C)
Anonymous inner classes require explicit constructors upon declaration!

D)
None of these statements are correct!
Answer: B

65. What happens when you attempt to create an instance of an interface directly?

A)
It will compile successfully but throw an error at runtime!

B)
It will compile successfully and allow instantiation without issues!

C)
It will not compile since interfaces cannot be instantiated directly!

D)
None of these statements are correct!
Answer: C

66. Which statement about lambda expressions in Java is true?



A)
Lambda expressions allow defining anonymous functions without needing full-
fledged implementations explicitly declared beforehand!

B)
Lambda expressions require explicit return types specified upfront during
declaration!

C)
Lambda expressions cannot capture variables from their surrounding scope
dynamically during execution contextually!

D)
None of these statements are correct!
Answer: A

Sure! Here are the multiple-choice questions with their answers and explanations:

▎Basic Java Concepts

1. What is the correct way to define a class in Java?

• A) class MyClass { }

• B) MyClass class { }

• C) class: MyClass { }

• D) define class MyClass { }

• Answer: A) class MyClass { }

• Explanation: In Java, classes are defined using the class keyword followed by
the class name and curly braces that contain the class body.

2. Which of the following statements is true about Java?

• A) Java is a platform-dependent language.


• B) Java is a platform-independent language.

• C) Java does not support OOP.

• D) Java can only run on Windows.

• Answer: B) Java is a platform-independent language.

• Explanation: Java is designed to be platform-independent through the use of the


Java Virtual Machine (JVM), which allows Java programs to run on any device
that has a JVM installed.

▎Data Types and Variables

3. Which of the following is not a primitive data type in Java?

• A) int

• B) char

• C) String

• D) boolean

• Answer: C) String

• Explanation: In Java, primitive data types include int, char, boolean, etc. String
is an object, not a primitive type.

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


int x = 5;
System.out.println(x++);
• A) 5

• B) 6

• C) 0

• D) Compilation error

• Answer: A) 5

• Explanation: The x++ operation returns the current value of x (which is 5) and
then increments it. Therefore, the output is 5.

▎Control Structures

5. Which statement is used to exit a loop in Java?

• A) continue

• B) break

• C) exit

• D) return

• Answer: B) break

• Explanation: The break statement is used to exit from a loop or switch


statement prematurely.

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


for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
}
• A) 1 2 3

• B) 0 1 2

• C) 0 1 2 3

• D) 1 2

• Answer: B) 0 1 2

• Explanation: The loop starts at i = 0 and runs while i < 3. It prints values of i
from 0 to 2.

▎Object-Oriented Programming (OOP)

7. Which keyword is used to inherit a class in Java?

• A) implements

• B) inherits

• C) extends

• D) super

• Answer: C) extends

• Explanation: The extends keyword is used in Java to indicate that one class is
inheriting from another class.

8. Which of the following statements is true about interfaces in Java?

• A) A class can implement multiple interfaces.


• B) An interface can extend another interface.

• C) An interface cannot have method bodies.

• D) All of the above.

• Answer: D) All of the above.

• Explanation: In Java, a class can implement multiple interfaces, an interface


can extend other interfaces, and interfaces cannot have method bodies (prior to
Java 8).

▎Exception Handling

9. What will happen if you divide a number by zero in Java?

• A) It will throw an ArithmeticException.

• B) It will return zero.

• C) It will return infinity.

• D) It will crash the program.

• Answer: A) It will throw an ArithmeticException.

• Explanation: In Java, dividing by zero results in an ArithmeticException, which


indicates an illegal arithmetic operation.

10. Which of the following is true about finally block in Java?

• A) It is executed only if an exception occurs.

• B) It is executed whether an exception occurs or not.


• C) It cannot be used with try-catch blocks.

• D) It can only be used with checked exceptions.

• Answer: B) It is executed whether an exception occurs or not.

• Explanation: The finally block is always executed after the try-catch blocks,
regardless of whether an exception was thrown or caught.

▎Collections Framework

11. Which of the following classes implements a LIFO (Last In First Out) data
structure?

• A) ArrayList

• B) LinkedList

• C) Stack

• D) HashMap

• Answer: C) Stack

• Explanation: The Stack class in Java implements a Last In First Out (LIFO)
data structure, meaning that the last element added is the first one to be removed.

12. What does the add() method do in a List?

• A) Removes an element from the List.

• B) Appends an element to the end of the List.

• C) Inserts an element at a specified index.


• D) Clears all elements from the List.

• Answer: B) Appends an element to the end of the List.

• Explanation: The add() method in a List interface adds an element to the end of
the list.

▎Multithreading

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

• A) run()

• B) execute()

• C) start()

• D) begin()

• Answer: C) start()

• Explanation: The start() method is called on a Thread object to initiate its


execution in a new thread.

14. What is the purpose of synchronized keyword in Java?

• A) To define a thread-safe class.

• B) To control access to shared resources.

• C) To pause a thread execution.

• D) To terminate a thread.

• Answer: B) To control access to shared resources.


• Explanation: The synchronized keyword ensures that only one thread can
access a block of code or an object at a time, preventing concurrent modification
issues.

▎Miscellaneous

15. What does the keyword final mean when applied to a variable?

• A) The variable can be changed.

• B) The variable cannot be changed after initialization.

• C) The variable can only be accessed within its own class.

• D) The variable can only be used as a constant.

• Answer: B) The variable cannot be changed after initialization.

• Explanation: When a variable is declared as final, its value cannot be modified


once it has been assigned.

16. What is garbage collection in Java?

• A) The process of cleaning up unused objects.

• B) The process of freeing memory manually.

• C) The process of optimizing code.

• D) The process of compiling code.

• Answer: A) The process of cleaning up unused objects.


• Explanation: Garbage collection is an automatic memory management process
that reclaims memory occupied by objects that are no longer referenced.

▎Advanced Topics

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


String str = "Hello";
System.out.println(str.substring(0, 2));

• A) Hel

• B) He

• C) Hello

• D) H

• Answer: B) He

• Explanation: The substring(0, 2) method returns the substring starting from


index 0 up to (but not including) index 2, which results in "He".

18. Which of the following is not a feature of Java?

• A) Platform independence

• B) High performance

• C) Low security

• D) Multithreading

• Answer: C) Low security


• Explanation: Java provides high security features such as bytecode verification
and access control, making it secure compared to many other languages.

19. What is the purpose of @Override annotation in Java?

• A) To indicate that a method is overloaded.

• B) To indicate that a method overrides a superclass method.

• C) To indicate that a method cannot be overridden.

• D) To indicate that a method is private.

• Answer: B) To indicate that a method overrides a superclass method.

• Explanation: The @Override annotation informs the compiler that the method
is intended to override a method in its superclass, helping catch errors if no such
method exists.

20. What is an immutable object in Java?

• A) An object that can change its state after creation.

• B) An object that cannot be changed after creation.

• C) An object that can only be created once.


• D) An object that does not have any methods.

• Answer: B) An object that cannot be changed after creation.

• Explanation: Immutable objects, such as instances of the String class, cannot


have their state modified once they are created, ensuring consistency and thread-
safety.
Below are 40 questions, each with four answer choices, along with the correct
answers and explanations.

---

▎String Handling

1. What is the result of "hello".length() in Java?

• A) 4
• B) 5
• C) 6
• D) Null

Answer: B) 5
Explanation: The length() method returns the number of characters in the string
"hello", which is 5.

---

2. Which method is used to find the index of a character in a string?

• A) indexOf()
• B) findIndex()
• C) charIndex()
• D) locate()

Answer: A) indexOf()
Explanation: The indexOf() method returns the index of the first occurrence of a
specified character in a string.

---

▎File Handling
3. Which class is used to create a file in Java?

• A) FileWriter
• B) File
• C) FileOutputStream
• D) FileCreator

Answer: B) File
Explanation: The File class is used to create, delete, and manipulate files and
directories.

---

4. What will be the output of this code snippet?


File file = new File("example.txt");
System.out.println(file.canRead());

Assuming "example.txt" exists and is readable:

• A) true
• B) false
• C) Exception
• D) null

Answer: A) true
Explanation: The canRead() method checks if the file can be read, returning true
if it exists and is readable.

---

▎Collections

5. Which interface does not allow duplicate elements?

• A) List
• B) Set
• C) Queue
• D) Map

Answer: B) Set
Explanation: The Set interface in Java does not allow duplicate elements.

---

6. Which class implements the List interface?

• A) HashSet
• B) ArrayList
• C) HashMap
• D) TreeSet

Answer: B) ArrayList
Explanation: ArrayList is a resizable array implementation of the List interface.

---

▎Object-Oriented Programming

7. What keyword is used to inherit a class in Java?

• A) inherit
• B) extends
• C) implements
• D) derives

Answer: B) extends
Explanation: The extends keyword is used to inherit a class in Java.

---
8. Which of the following is a feature of encapsulation?

• A) Data hiding
• B) Inheritance
• C) Polymorphism
• D) Abstraction

Answer: A) Data hiding


Explanation: Encapsulation involves restricting access to certain components and
hiding data from direct access.

---

▎Exception Handling

9. What type of exception is thrown when an array is accessed with an illegal


index?

• A) NullPointerException
• B) ArrayIndexOutOfBoundsException
• C) ClassCastException
• D) ArithmeticException

Answer: B) ArrayIndexOutOfBoundsException
Explanation: This exception is thrown when an attempt is made to access an
array with an index that is out of bounds.

---

10. Which block is executed when an exception is thrown?

• A) try block
• B) catch block
• C) finally block
• D) all of the above
Answer: D) all of the above
Explanation: The try block contains code that may throw an exception, the catch
block handles it, and the finally block executes regardless of whether an exception
occurred.

---

▎Miscellaneous Concepts

11. Which keyword is used to declare a constant variable in Java?

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

Answer: B) final
Explanation: The final keyword is used to declare constants in Java.

---

12. What does the 'static' keyword signify in Java?

• A) Instance variable
• B) Class variable
• C) Local variable
• D) Global variable

Answer: B) Class variable


Explanation: A static variable belongs to the class rather than instances of the
class.

---
▎Multithreading

ChatGPT 4 | Midjourney | Claude | Suno, [12/13/2024 2:33 PM]


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

• A) run()
• B) start()
• C) execute()
• D) begin()

Answer: B) start()
Explanation: The start() method is used to initiate thread execution.

---

14. What does the 'join()' method do in Java threads?

• A) Waits for a thread to die


• B) Starts a new thread
• C) Suspends a thread's execution
• D) Terminates a thread immediately

Answer: A) Waits for a thread to die


Explanation: The join() method allows one thread to wait for another thread to
complete its execution.

---

▎Networking

15. Which class is used for creating a socket connection in Java?

• A) SocketConnection
• B) Socket
• C) ServerSocket
• D) DatagramSocket

Answer: B) Socket
Explanation: The Socket class is used for creating client socket connections in
Java.

---

16. What protocol does Java use for web communication?

• A) FTP
• B) HTTP/HTTPS
• C) SMTP
• D) SNMP

Answer: B) HTTP/HTTPS
Explanation: HTTP (Hypertext Transfer Protocol) and HTTPS (HTTP Secure)
are standard protocols for web communication.

---

▎Database Connectivity

17. Which package is used for JDBC in Java?

• A) java.sql
• B) java.dbc
• C) java.database
• D) java.jdbc

Answer: A) java.sql
Explanation: The java.sql package contains classes and interfaces for JDBC
(Java Database Connectivity).

---
▎Annotations

18. Which annotation is used to indicate that a method overrides a superclass


method?

• A) @Override
• B) @OverrideMethod
• C) @OverrideFunction
• D) @MethodOverride

Answer: A) @Override
Explanation: The @Override annotation indicates that a method is intended to
override a method declared in a superclass.

---

▎Lambda Expressions

19. What is the primary purpose of lambda expressions in Java?

• A) To create anonymous classes only


• B) To simplify functional programming and enable concise code for functional
interfaces
• C) To replace all classes in Java with functions only
• D) To increase compilation time significantly

Answer: B) To simplify functional programming and enable concise code for


functional interfaces
Explanation: Lambda expressions provide a clear and concise way to represent
one method interfaces using an expression, thus simplifying functional
programming.

---
▎Streams

20. Which method is used to filter elements in a Stream?

• A) filter()
• B) map()
• C) collect()
• D) reduce()

Answer: A) filter()
Explanation: The filter() method allows you to specify a condition that elements
must meet to be included in the resulting stream.

---

▎Generics

21. What does the diamond operator <> signify in Java?

• A) It indicates that a class can have multiple types.


• B) It simplifies the syntax for generics.
• C) It denotes an abstract class.
• D) It indicates that no type has been specified.

Answer: B) It simplifies the syntax for generics.


Explanation: The diamond operator allows you to omit type parameters when
they can be inferred from context, simplifying generic declarations.

---

▎Interfaces

ChatGPT 4 | Midjourney | Claude | Suno, [12/13/2024 2:33 PM]


22. Which keyword is used to implement an interface in Java?
• A) implements
• B) uses
• C) extends
• D) inherits

Answer: A) implements
Explanation: The implements keyword is used by a class to inherit from an
interface.

---

▎Access Modifiers

23. Which access modifier allows access only within its own package?

• A) public
• B) private
• C) protected
• D) default (package-private)

Answer: D) default (package-private)


Explanation: If no access modifier is specified, it defaults to package-private,
allowing access only within its own package.

---

▎Static Methods

24. Can static methods access instance variables directly?

• A) Yes, always
• B) No, they cannot access instance variables directly without an object
reference.
• C) Yes, but only if declared as final.
• D) No, static methods cannot access any variables.
Answer: B) No, they cannot access instance variables directly without an object
reference.
Explanation: Static methods belong to the class and do not have access to
instance variables unless they create an instance of the class.

---

▎Constructors

25. What happens if no constructor is defined in a class?

• A) Compilation error occurs.


• B) A default constructor is provided by the compiler.
• C) The program will not run.
• D) An exception will be thrown at runtime.

Answer: B) A default constructor is provided by the compiler.

---

▎Enums

26. How do you define an enum in Java?

• A ) enum Days { SUNDAY, MONDAY, TUESDAY }

• B ) enum Days: { SUNDAY, MONDAY, TUESDAY }

• C ) Days enum { SUNDAY, MONDAY, TUESDAY }

• D ) Days: enum { SUNDAY, MONDAY, TUESDAY }


• Answer: A ) enum Days { SUNDAY, MONDAY, TUESDAY }

• Explanation: Enums are defined using the enum keyword followed by the
name and its constants enclosed in braces.

---

▎HashMap

27. Which method retrieves a value from a HashMap?

• A ) getValue()

• B ) fetch()

• C ) get()

• D ) retrieve()

• Answer: C ) get()

• Explanation: The get() method retrieves the value associated with a specified
key from a HashMap.

---

▎StringBuilder

28. Which class is mutable: String or StringBuilder?

• A ) String
• B ) StringBuilder

• C ) Both are mutable

• D ) Neither is mutable

• Answer: B ) StringBuilder

• Explanation: StringBuilder objects are mutable, meaning their contents can be


changed after creation.

---

▎Threads

29. What does synchronized keyword do in multithreading?

• A ) It creates multiple threads.

• B ) It allows only one thread to access a resource at a time.

• C ) It stops all threads.

• D ) It prioritizes threads.

• Answer: B ) It allows only one thread to access a resource at a time.

• Explanation: The synchronized keyword ensures that only one thread can
access a block of code or object at any given time.

---
▎Reflection

30. What does reflection allow you to do in Java?

• A ) Change private variables.

• B ) Access metadata about classes and objects at runtime.

• C ) Create new classes at runtime.

• D ) All of the above.

• Answer: D ) All of the above.

• Explanation: Reflection allows you to inspect classes, interfaces, fields, and


methods at runtime.

---

▎Annotations

31. What annotation indicates that a method should not be overridden?

• A ) @NotOverridden

• B ) @Final

• C ) @Static

• D ) @Abstract
• Answer: B ) @Final

• Explanation: The @Final annotation (or using final keyword on methods in


general), indicates that a method cannot be overridden.

---

▎Serialization

32. What must a class implement to support serialization?

• A ) Serializable interface

• B ) Cloneable interface

• C ) Externalizable interface

• D ) Both A and C

• Answer: A ) Serializable interface

• Explanation: Classes must implement Serializable interface for their objects to


be serialized.

---

▎Method Overloading

33. What happens during method overloading?

• A ) Methods have different names.


• B ) Methods have different return types.

• C ) Methods have different parameter lists.

• D ) All of the above.

• Answer: C ) Methods have different parameter lists.

• Explanation: Method overloading occurs when multiple methods have the


same name but different parameter lists.

---

▎Method Overriding

34. What happens during method overriding?

• A ) Subclass provides specific implementation for a method already defined in


its superclass.

• B ) Subclass cannot change superclass methods.

• C ) Superclass methods are deleted.

• D ) Method names must be different.

• Answer: A ) Subclass provides specific implementation for a method already


defined in its superclass.

• Explanation: Method overriding allows subclasses to provide specific


implementations of methods defined in their superclass.
---

▎Inner Classes

35. What can inner classes access?

• A ) Only static members of outer classes.

• B ) Only public members of outer classes.

• C ) All members (including private members).

• D ) Only members inherited from outer classes.

• Answer: C ) All members (including private members).

• Explanation: Inner classes can access all members (including private members
and methods).

---

▎Static Blocks

36. When are static blocks executed?

• A ) When an instance of the class is created.

• B ) When the class is loaded into memory.

• C ) When static methods are called.


• D ) At runtime only when needed.

• Answer: B ) When the class is loaded into memory.

• Explanation: Static blocks are executed when the class is loaded into memory.

---

▎Default Methods

37. What feature was introduced in Java 8 regarding interfaces?

• A ) Abstract methods only.

• B ) Default methods with implementation.

• C ) Static variables only.

• D ) Private methods only.

• Answer: B ) Default methods with implementation.

• Explanation: Java 8 introduced default methods that allow interfaces to have


methods with implementations.

---

▎Optional Class

38. What does Optional<T> represent in Java?


– A ) A container object which may or may not contain a non-null value.
– B ) An array that can contain null values.
– C ) An interface for handling exceptions.
– D ) A special type of collection.

– Answer: A ) A container object which may or may not contain a non-null


value.
– Explanation: Optional<T> is used to represent optional values that can be
either present or absent.

---

▎Streams API

39. Which operation does not modify the original Stream?

– A ) map()
– B ) filter()
– C ) sorted()
– D ) all of these modify original Stream

– Answer: D ) all of these modify original Stream


– Explanation: Operations like map(), filter(), and sorted() return new streams
without altering the original stream.

---

▎Java Virtual Machine (JVM)

40. What does JVM stand for?


– A ) Java Variable Machine
– B ) Java Virtual Machine
– C ) Java Version Manager
– D ) Java Visual Machine

– Answer: B ) Java Virtual Machine


– Explanation: JVM stands for Java Virtual Machine; it enables Java bytecode to
be executed on any platform.

---

These questions cover various fundamental concepts in Java programming along


with their respective answers and explanations! If you need more information or
have further questions, feel free to ask!

You might also like