- Question: What is Java?
- A) A scripting language
- B) A programming language
- C) A markup language
- D) An operating system
- Answer: B) A programming language
- Question: What is the JVM?
- A) Java Virtual Machine
- B) Java Variable Manager
- C) Java Version Manager
- D) Java Visual Model
- Answer: A) Java Virtual Machine
- Question: Which keyword is used to define a class in Java?
- A) class
- B) Class
- C) Class
- D) category
- Answer: A) class
- Question: What is the purpose of the ‘static’ keyword in Java?
- A) To define a constant
- B) To specify a method or variable as class-level
- C) To prevent inheritance
- D) To declare a method as private
- Answer: B) To specify a method or variable as class-level
- Question: Which of the following is NOT a primitive data type in Java?
- A) int
- B) float
- C) string
- D) char
- Answer: C) string
- Question: What is the output of the following code snippet?
String str = "Hello";
str = str + " World!";
System.out.println(str);
- A) Hello World!
- B) Hello
- C) World!
- D) Compilation Error
- Answer: A) Hello World!
- Question: What is the default value of an integer variable in Java?
- A) 0
- B) 1
- C) -1
- D) null
- Answer: A) 0
- Question: Which keyword is used to instantiate an object in Java?
- A) instantiate
- B) new
- C) create
- D) alloc
- Answer: B) new
- Question: What is the difference between ‘==’ and ‘.equals()’ in Java?
- A) They are interchangeable
- B) ‘==’ compares object references, ‘.equals()’ compares object contents
- C) ‘.equals()’ compares object references, ‘==’ compares object contents
- D) Both do the same thing
- Answer: B) ‘==’ compares object references, ‘.equals()’ compares object contents
- Question: Which of the following is NOT a valid Java identifier?
- A) myVar
- B) 123var
- C) _myVar
- D) $myVar
- Answer: B) 123var
- Question: What is method overloading in Java?
- A) Creating multiple methods with the same name but different parameters
- B) Creating multiple methods with the same name and same parameters
- C) Creating multiple methods with different names but the same parameters
- D) Creating multiple methods with different return types
- Answer: A) Creating multiple methods with the same name but different parameters
- Question: What is method overriding in Java?
- A) Creating multiple methods with the same name but different parameters
- B) Creating multiple methods with the same name and same parameters
- C) Creating multiple methods with different names but the same parameters
- D) Redefining a method in a subclass with the same signature as a method in the superclass
- Answer: D) Redefining a method in a subclass with the same signature as a method in the superclass
- Question: What is the purpose of the ‘final’ keyword in Java?
- A) To prevent a class from being extended
- B) To prevent a method from being overridden
- C) To prevent a variable from being modified
- D) All of the above
- Answer: D) All of the above
- Question: What is an abstract class in Java?
- A) A class with only abstract methods
- B) A class that cannot be instantiated
- C) A class that can be extended but not instantiated
- D) A class with no methods
- Answer: C) A class that can be extended but not instantiated
- Question: What is an interface in Java?
- A) A class with only abstract methods
- B) A class that cannot be instantiated
- C) A class that can be extended but not instantiated
- D) A blueprint of a class that can be implemented by other classes
- Answer: D) A blueprint of a class that can be implemented by other classes
- Question: What is the purpose of the ‘this’ keyword in Java?
- A) To refer to the current class instance variable
- B) To create a new instance of a class
- C) To refer to the superclass
- D) To refer to the current class method
- Answer: A) To refer to the current class instance variable
- Question: What is the purpose of the ‘super’ keyword in Java?
- A) To refer to the current class instance variable
- B) To create a new instance of a class
- C) To refer to the superclass
- D) To refer to the current class method
- Answer: C) To refer to the superclass
- Question: What is the purpose of the ‘throw’ keyword in Java?
- A) To declare a variable
- B) To create a new instance of an object
- C) To explicitly throw an exception
- D) To catch an exception
- Answer: C) To explicitly throw an exception
- Question: What is the purpose of the ‘throws’ keyword in Java?
- A) To declare a variable
- B) To create a new instance of an object
- C) To explicitly throw an exception
- D) To declare that a method may throw an exception
- Answer: D) To declare that a method may throw an exception
- Question: What is a constructor in Java?
- A) A method that has no return type
- B) A method that is called when an object is instantiated
- C) A method that is called when an object is destroyed
- D) A method that has the same name as the class
- Answer: B) A method that is called when an object is instantiated
- Question: What is the purpose of the ‘finalize’ method in Java?
- A) To initialize an object
- B) To de-initialize an object
- C) To clean up resources before an object is garbage collected
- D) To catch exceptions
- Answer: C) To clean up resources before an object is garbage collected
22.
Question: What is the purpose of the ‘static’ block in Java?
– A) To initialize static variables
– B) To initialize instance variables
– C) To define static methods
– D) To define instance methods
– Answer: A) To initialize static variables
- Question: What is the purpose of the ‘transient’ keyword in Java?
- A) To prevent serialization of a variable
- B) To allow serialization of a variable
- C) To prevent a class from being serialized
- D) To allow a class to be serialized
- Answer: A) To prevent serialization of a variable
- Question: What is the purpose of the ‘volatile’ keyword in Java?
- A) To prevent multiple threads from accessing a variable at the same time
- B) To ensure that changes to a variable are immediately visible to other threads
- C) To prevent a variable from being modified
- D) To ensure that a variable is initialized before it is accessed
- Answer: B) To ensure that changes to a variable are immediately visible to other threads
- Question: What is the purpose of the ‘synchronized’ keyword in Java?
- A) To prevent multiple threads from accessing a variable at the same time
- B) To ensure that changes to a variable are immediately visible to other threads
- C) To prevent a variable from being modified
- D) To ensure that a variable is initialized before it is accessed
- Answer: A) To prevent multiple threads from accessing a variable at the same time
- Question: What is a thread in Java?
- A) A process
- B) A lightweight process
- C) A class
- D) A variable
- Answer: B) A lightweight process
- Question: What is the purpose of the ‘start()’ method in Java threads?
- A) To start a new thread
- B) To stop a thread
- C) To pause a thread
- D) To resume a thread
- Answer: A) To start a new thread
- Question: What is the purpose of the ‘sleep()’ method in Java threads?
- A) To start a new thread
- B) To stop a thread
- C) To pause a thread
- D) To resume a thread
- Answer: C) To pause a thread
- Question: What is the purpose of the ‘yield()’ method in Java threads?
- A) To start a new thread
- B) To stop a thread
- C) To pause a thread
- D) To give up the CPU temporarily
- Answer: D) To give up the CPU temporarily
- Question: What is the purpose of the ‘join()’ method in Java threads?
- A) To start a new thread
- B) To stop a thread
- C) To pause a thread
- D) To wait for a thread to finish
- Answer: D) To wait for a thread to finish
- Question: What is the purpose of the ‘notify()’ method in Java threads?
- A) To wake up a single waiting thread
- B) To wake up all waiting threads
- C) To put a thread to sleep
- D) To stop a thread
- Answer: A) To wake up a single waiting thread
- Question: What is the purpose of the ‘notifyAll()’ method in Java threads?
- A) To wake up a single waiting thread
- B) To wake up all waiting threads
- C) To put a thread to sleep
- D) To stop a thread
- Answer: B) To wake up all waiting threads
- Question: What is the purpose of the ‘wait()’ method in Java threads?
- A) To wake up a single waiting thread
- B) To wake up all waiting threads
- C) To put a thread to sleep
- D) To stop a thread
- Answer: C) To put a thread to sleep
- Question: What is the purpose of the ‘interrupt()’ method in Java threads?
- A) To wake up a single waiting thread
- B) To wake up all waiting threads
- C) To put a thread to sleep
- D) To stop a thread
- Answer: D) To stop a thread
- Question: What is the purpose of the ‘isAlive()’ method in Java threads?
- A) To check if a thread is running
- B) To check if a thread is paused
- C) To check if a thread is sleeping
- D) To check if a thread is terminated
- Answer: A) To check if a thread is running
- Question: What is the purpose of the ‘isInterrupted()’ method in Java threads?
- A) To check if a thread is running
- B) To check if a thread is paused
- C) To check if a thread is sleeping
- D) To check if a thread has been interrupted
- Answer: D) To check if a thread has been interrupted
- Question: What is the purpose of the ‘getName()’ method in Java threads?
- A) To get the name of a thread
- B) To set the name of a thread
- C) To get the ID of a thread
- D) To set the priority of a thread
- Answer: A) To get the name of a thread
- Question: What is the purpose of the ‘setName()’ method in Java threads?
- A) To get the name of a thread
- B) To set the name of a thread
- C) To get the ID of a thread
- D) To set the priority of a thread
- Answer: B) To set the name of a thread
- Question: What is the purpose of the ‘getPriority()’ method in Java threads?
- A) To get the name of a thread
- B) To set the name of a thread
- C) To get the ID of a thread
- D) To get the priority of a thread
- Answer: D) To get the priority of a thread
- Question: What is the purpose of the ‘setPriority()’ method in Java threads?
- A) To get the name of a thread
- B) To set the name of a thread
- C) To get the ID of a thread
- D) To set the priority of a thread
- Answer: D) To set the priority of a thread
- Question: What is the purpose of the ‘yield()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To wait for a thread to finish
- D) To wake up a waiting thread
- Answer: A) To give up the CPU temporarily
- Question: What is the purpose of the ‘sleep()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To wait for a thread to finish
- D) To wake up a waiting thread
- Answer: C) To wait for a thread to finish
- Question: What is the purpose of the ‘join()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To wait for a thread to finish
- D) To wake up a waiting thread
- Answer: C) To wait for a thread to finish
- Question: What is the purpose of the ‘notify()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To wake up a waiting thread
- D) To put a thread to sleep
- Answer: C) To wake up a waiting thread
- Question: What is the purpose of the ‘notifyAll()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To wake up all waiting threads
- D) To put a thread to sleep
- Answer: C) To wake up all waiting threads
- Question: What is the purpose of the ‘interrupt()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To stop a thread
- D) To put a thread to sleep
- Answer: C) To stop a thread
- Question: What is the purpose of the ‘yield()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To stop a thread
- D) To wake up a waiting thread
- Answer: A) To give up the CPU temporarily
- Question: What is the purpose of the ‘sleep()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To stop a thread
- D) To wait for a thread to finish
- Answer: A) To give up the CPU temporarily
- Question: What is the purpose of the ‘join()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To stop a thread
- D) To wait for a thread to finish
- Answer: D) To wait for a thread to finish
- Question: What is the purpose of the ‘notify()’ method in Java threads?
- A) To give up the CPU temporarily
- B) To give up the CPU permanently
- C) To wake up a waiting thread
- D) To stop a thread
- Answer: C) To wake up a waiting thread
These questions cover a broad range of topics related to Java programming and multithreading. They can help assess a candidate’s understanding and proficiency in Java development.