1.
which term accurately describes the relationship established through inheritance in
object-oriented-programming ?
a) has-a relationship
b) Is-the relationship
c) Is-A relationship
d) Interface relationship
2. Which of the following options represents the main components of exception handling in java
a) Try ,catch,if,else,switch
b) Exception ,throw , catch,throws, try
c) Try,catch,final, throw,throws
d) Try,catch,throw,throws,finally
3. The wrapper class in java provides the mechanism to convert primitive into object and object
into _
a) Data
b) Primitive
c) Variable
d) Non-primitive
4. What will be output of the below code?
public class Main
{
public static int Sum(int a, int b) {
return a+b;
}
public static int Sum(int a, int b, int c){
return a+b+c;
}
public static void main(String[] args) {
System.out.println(Sum(11,11));
System.out.println(Sum (11,11,11));
}
}
A) 11
11
B) Compile-Time Error
C) 22
33
D) 121
1331
5) Which among the following option can be used to make String mutable?
A) Declaring it as 'final'
B) Wrapping it in a StringBuffer
C) Applying the 'mutable' modifier
D) Using the 'const' keyword
6) class A
{
public A(String s)
{
System.out.print("A")
}
}
public class B extends A
{
public B(String s)
{
System.out.print("B");
}
public static void main(String[] args)
{
new B("C");
System.out.println(" ");
}
}
Output: Compilation Error
Explanation: The implied super() call in B's constructor cannot be satisfied because
there isn't a no-arg constructor in A. A default, no-arg constructor is generated by the
compiler only if the class has no constructor defined explicitly.
7) Predict the output of the following code.
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
Output : Compile-Time Error
final method can't be overriden
8) Predict the output of the below code?
(Lambda expression with or without return keyword)
interface Addable{
int add(int a,int b);
}
public class LambdaExpressionExample6 {
public static void main(String[] args) {
// Lambda expression without return keyword.
Addable ad1 = (a,b)->(a+b);
System.out.println(ad1.add(10,20));
// Lambda expression with return keyword.
Addable ad2=(int aint b)->{
return (a+b);
};
System.out.println(ad2.add(100,200));
}
}
Output:
30
300
9) in java thread can be created by _ :
Extending the thread class OR implementing runnable interface
10) What is the output of the following java program?
class Test
{
public static void main(String[] args)
{
try
{
int a[] {1, 2, 3, 4};
for (int i = 1; i <= 4; i++)
{
System.out.println("a["is à +"]="+a[i] + "n");
}
}
catch (Exception e)
{
System.out.println ("error = " + e);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println ("ArrayIndexOutOfBoundsException");
}
}
}
Options:
A) Compiler error
B) Run time error
C) ArrayIndexOutOfBoundsException
D) Error Code is printed
E) Array is printed
Answer:
A) Compiler error Explanation:
ArrayIndexOutOfBoundsException has been already caught by base class Exception.
When a subclass exception is mentioned after base class exception, then error occurs.
11) class Test {
psvm(){
try{
int a = args.length();
int b = 10/a;
sop("passed");
}
catch(ArithmeticException e){
sop("Failed");
}
}
}
Failed
passed
0
10
12) abstract class Employee{
abstract void attendance(); //line 1
}
class Honda extends Employee{
void attendance(){
sop("25 days"); //line 2
}
psvm(){
Employee obj = new Honda(); //line 3
obj.attendance();
}
}
compile time error line 1
25 days
compile time error at line 2
compile time eroor ar line 3
13) class Example{
psvm(){
int[] arr = {12,28,56,33,46};
#missing code
}
}
Output:
12
28
56
33
46
a) for(int i:arr){
sop(i);
}
b) for(int i =0;arr.length){
sop(i);
}
c) for(int i=0;arr.length;i++){
sop(i);
}
14) class splitExample{
psvm(){
String s1 = 'IT Technical';
String[] words = s1.split("\\s");
for(String w:words){
sop(w);
}
}
}
Output:
IT
Technical
15) class Main{
static void small(int a){
if(a<18){
throw new ArithmeticException("Smaller Number");
}
else{
sop("larger number");
}
}
psvm(){
small(25);
}
}
a) Larger Number
b) Smaller Number
c) Larger Number:25
d) Smaller Number:18
16) which statement is correct if we want to connect the oracle database using the thin driver
provided by Oracle Corp.?
getConnection("jdbc::thin@localhost:1521:oracle","scott","tiger");
getConnection("jdbc:thin@localhost:1521:oracle","scott","tiger");
getConnection("jdbc::thin@localhost:1522:oracle","scott","tiger");
getConnection("jdbc::oracle@localhost:1521:thin","scott","tiger");
17) which of the following method is used to perform DML statemets in jdbc?
executeResult()
executeQuery()
executeUpdate()
execute()
18) class Value{
psvm(){
short x = 32767
x ++;
x ++;
sop(x);
}
}
32767
-32767
0
32769
19) class Test{
psvm(){
try{
int x,y;
x = 25;
y = 0;
x = (y/5)*x;
sop("A");
}
catch(ArithmeticException e){
sop("B");
}
}
}
A
B
0
ArithmeticException: Divide by Zero
20) what will be the output of the following code ?
import java.util.*;
class Bitset{
psvm{
BitSet b = new BitSet(5);
for(int j=0;j<5;++j){
b.set(j*2);
b.clear(4);
sop(b);
}
}
}
{0,2,6,8}
{0,2,4,6}
{0,2,4,8}
{4}
21) char n = '1', // if you give n = '12' it gives error since it
contain more than 1 literal
prints value contained in n
char n = 12
prints AASCI Code of 12
AASCI code 65 = A .....So on
In Java, a char type can only hold a single character within single quotes.
22) psvm(String args[])
args.length():
will always be zero
23) Basic Level
1. Which of the following is not a primitive data type in Java?
- A. int
- B. float
- C. string
- D. boolean
- **Answer: C. string**
2. What is the default value of the boolean datatype in Java?
- A. true
- B. false
- C. 0
- D. 1
- **Answer: B. false**
3. Which keyword is used to define a constant in Java?
- A. const
- B. final
- C. static
- D. constant
- **Answer: B. final**
4. What is the output of the following code snippet?
int x = 5;
System.out.println(x++ + ++x);
- A. 10
- B. 11
- C. 12
- D. 13
- **Answer: D. 13**
5. Which of the following is a valid declaration of a String in Java?
- A. String s1 = null;
- B. String s2 = 'Hello';
- C. String s3 = new String("Hello");
- D. String s4 = 12345;
- **Answer: A. String s1 = null;**
6. What is the result of 10 % 3 in Java?
- A. 0
- B. 1
- C. 2
- D. 3
- **Answer: B. 1**
7. What is the output of the following code snippet?
String str1 = "hello";
String str2 = "world";
System.out.println(str1 + str2);
- A. helloworld
- B. hello world
- C. hello + world
- D. Compilation Error
- **Answer: A. helloworld**
8. Which keyword is used to instantiate an object in Java?
- A. new
- B. create
- C. object
- D. instance
- **Answer: A. new**
9. What is the parent class of all classes in Java?
- A. Object
- B. Super
- C. Base
- D. Main
- **Answer: A. Object**
10. Which of the following statements is used to exit from a loop in Java?
- A. return
- B. exit
- C. break
- D. continue
- **Answer: C. break**
11. What will be the output of the following code snippet?
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is less than or equal to 5");
}
- A. x is greater than 5
- B. x is less than or equal to 5
- C. Compilation Error
- D. Runtime Error
- **Answer: A. x is greater than 5**
12. Which data structure is used to implement a Last-In-First-Out (LIFO) mechanism in Java?
- A. Queue
- B. Stack
- C. Array
- D. Linked List
- **Answer: B. Stack**
13. What is the output of the following code snippet?
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr.length);
- A. 5
- B. 4
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
14. Which of the following is a loop construct in Java?
- A. for-if
- B. do-while
- C. while-if
- D. repeat-until
- **Answer: B. do-while**
15. What is the output of the following code snippet?
int x = 5;
System.out.println(x > 2 ? "Yes" : "No");
- A. Yes
- B. No
- C. true
- D. false
- **Answer: A. Yes**
16. Which of the following is a valid identifier in Java?
- A. 123abc
- B. _abc123
- C. break
- D. 3abc
- **Answer: B. _abc123**
17. What will be the value of 'x' after executing the following code snippet?
int x = 5;
x += 3;
- A. 5
- B. 3
- C. 8
- D. 15
- **Answer: C. 8**
18. Which of the following is not a valid access modifier in Java?
- A. public
- B. protected
- C. private
- D. global
- **Answer: D. global**
19. What is the output of the following code snippet?
String str = "hello";
System.out.println(str.toUpperCase());
- A. HELLO
- B. hello
- C. Compilation Error
- D. Runtime Error
- **Answer: A. HELLO**
20. Which of the following is not a type of Java exception?
- A. Checked
- B. Unchecked
- C. Runtime
- D. Fatal
- **Answer: D. Fatal**
21. What is the output of the following code snippet?
int x = 10;
System.out.println(x / 0);
- A. Compilation Error
- B. Runtime Error
- C. 0
- D. Infinity
- **Answer: B. Runtime Error**
22. Which of the following is not a valid way to create an array in Java?
- A. int[] arr = new int[5];
- B. int[] arr = {1, 2, 3, 4, 5};
- C. int[] arr = new int[]{1, 2, 3, 4, 5};
- D. int[] arr = new int(5);
- **Answer: D. int[] arr = new int(5);**
23. What is the output of the following code snippet?
int x = 10;
if (x == 10)
System.out.println("x is 10");
System.out.println("End of if");
- A. x is 10 End of if
- B. x is 10
- C. Compilation
Error
- D. Runtime Error
- **Answer: B. x is 10**
24. Which of the following statements is used to skip the current iteration in a loop in Java?
- A. return
- B. break
- C. skip
- D. continue
- **Answer: D. continue**
25. What is the output of the following code snippet?
int[] arr = new int[5];
System.out.println(arr[0]);
- A. 0
- B. null
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 0**
26. Which of the following is not a valid statement in Java?
- A. if (x > 0) {}
- B. while (x < 5) {}
- C. void x = 10;
- D. System.out.println("Hello");
- **Answer: C. void x = 10;**
27. What will be the value of 'y' after executing the following code snippet?
int x = 5;
int y = x++;
- A. 5
- B. 6
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
28. Which of the following is a correct way to declare and initialize a two-dimensional array in
Java?
- A. int[][] arr = new int[3, 3];
- B. int[][] arr = new int[3][3];
- C. int[][] arr = {1, 2, 3, 4, 5};
- D. int[][] arr = new int{{1, 2, 3}, {4, 5, 6}};
- **Answer: B. int[][] arr = new int[3][3];**
29. What is the output of the following code snippet?
int x = 5;
System.out.println(++x);
- A. 5
- B. 6
- C. Compilation Error
- D. Runtime Error
- **Answer: B. 6**
30. Which of the following is a correct way to declare an arraylist in Java?
- A. ArrayList arr = new ArrayList();
- B. List arr = new List();
- C. ArrayList<String> arr = new ArrayList<String>();
- D. List<String> arr = new List<String>();
- **Answer: C. ArrayList<String> arr = new ArrayList<String>();**
31. What will be the output of the following code snippet?
int x = 5;
int y = 3;
System.out.println(x > y ? x : y);
- A. 5
- B. 3
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
32. Which of the following is a correct way to declare a method in Java?
- A. void myMethod() {}
- B. myMethod() {}
- C. public void myMethod() {}
- D. static void myMethod() {}
- **Answer: C. public void myMethod() {}**
33. What is the output of the following code snippet?
String str = "hello";
System.out.println(str.charAt(2));
- A. h
- B. e
- C. l
- D. o
- **Answer: C. l**
34. Which of the following is not a valid loop in Java?
- A. for loop
- B. while loop
- C. until loop
- D. do-while loop
- **Answer: C. until loop**
35. What is the output of the following code snippet?
int x = 5;
System.out.println(x--);
- A. 5
- B. 4
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
36. Which of the following is not a valid statement to create an object in Java?
- A. MyClass obj = new MyClass();
- B. new MyClass();
- C. obj = new MyClass();
- D. MyClass obj = MyClass();
- **Answer: D. MyClass obj = MyClass();**
37. What will be the output of the following code snippet?
int x = 5;
int y = 3;
System.out.println(x == y);
- A. true
- B. false
- C. Compilation Error
- D. Runtime Error
- **Answer: B. false**
38. Which of the following is a correct way to declare a constructor in Java?
- A. MyClass() {}
- B. void MyClass() {}
- C. public MyClass() {}
- D. static MyClass() {}
- **Answer: C. public MyClass() {}**
39. What is the output of the following code snippet?
String str = "hello";
System.out.println(str.length());
- A. 5
- B. 6
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
40. Which of the following is not a valid statement in Java?
- A. MyClass obj = null;
- B. MyClass obj = new MyClass();
- C. MyClass obj = MyClass();
- D. MyClass obj;
- **Answer: C. MyClass obj = MyClass();**
Intermediate Level
1. Which of the following statements about Java interfaces is true?
- A. Interfaces can contain constructors.
- B. A class can implement multiple interfaces with conflicting method signatures.
- C. Interface methods are by default final.
- D. Interfaces can have instance variables.
- **Answer: D. Interfaces can have instance variables.**
2. What does the `volatile` keyword ensure in Java?
- A. It prevents a variable from being modified.
- B. It ensures that a variable is accessed by only one thread at a time.
- C. It ensures visibility of changes to variables across threads.
- D. It specifies the initial value of a variable.
- **Answer: C. It ensures visibility of changes to variables across threads.**
3. Which collection class allows null elements in Java?
- A. HashSet
- B. TreeSet
- C. ArrayList
- D. LinkedList
- **Answer: A. HashSet**
4. What is the purpose of the `transient` keyword in Java?
- A. It indicates that a variable cannot be serialized.
- B. It prevents a variable from being modified.
- C. It specifies that a variable cannot be accessed by other classes.
- D. It indicates that a variable should not be persisted when its containing object is serialized.
- **Answer: D. It indicates that a variable should not be persisted when its containing object is
serialized.**
5. Which of the following is true about method overloading in Java?
- A. Method overloading occurs when two methods have the same name and the same
number of parameters.
- B. Method overloading is also known as compile-time polymorphism.
- C. Method overloading can be achieved by changing the return type of the method.
- D. Method overloading cannot be performed in abstract classes.
- **Answer: B. Method overloading is also known as compile-time polymorphism.**
6. What is the difference between `==` and `.equals()` method in Java?
- A. They both perform reference comparison.
- B. `==` compares the references of objects, while `.equals()` compares the contents of
objects.
- C. `.equals()` is used for primitive data types, while `==` is used for objects.
- D. They both perform value comparison.
- **Answer: B. `==` compares the references of objects, while `.equals()` compares the
contents of objects.**
7. Which of the following is a valid declaration of a static method in Java?
- A. `public void static myMethod() {}`
- B. `public static void myMethod() {}`
- C. `static void public myMethod() {}`
- D. `public myMethod() static {}`
- **Answer: B. `public static void myMethod() {}`**
8. What is the purpose of the `super()` keyword in Java?
- A. It refers to the superclass object.
- B. It is used to invoke the superclass constructor.
- C. It is used to call a method from the superclass.
- D. It is used to access static members of the superclass.
- **Answer: B. It is used to invoke the superclass constructor.**
9. Which of the following is not a valid access modifier in Java?
- A. private
- B. default
- C. protected
- D. global
- **Answer: D. global**
10. What is the purpose of the `this` keyword in Java?
- A. It refers to the current object.
- B. It is used to create a new instance of a class.
- C. It is used to refer to the superclass object.
- D. It is used to access static members of a class.
- **Answer: A. It refers to the current object.**
11. What does the `static` keyword mean when applied to a method in Java?
- A. It indicates that the method cannot be overridden.
- B. It indicates that the method belongs to the class rather than the instance.
- C. It indicates that the method cannot be accessed outside the class.
- D. It indicates that the method is abstract.
- **Answer: B. It indicates that the method belongs to the class rather than the instance.**
12. Which of the following is not a valid statement about Java threads?
- A. Threads can be created by extending the `Thread` class.
- B. Threads can be created by implementing the `Runnable` interface.
- C. Java applications can have only one thread.
- D. The `sleep()` method is used to pause the execution of a thread.
- **Answer: C. Java applications can have only one thread.**
13. What is the purpose of the `try-catch` block in Java?
- A. It is used to handle exceptions that occur during program execution.
- B. It is used to define a method.
- C. It is used to declare variables.
- D. It is used to define a loop.
- **Answer: A. It is used to handle exceptions that occur during program execution.**
14. Which of the following is true about Java packages?
- A. A package can contain only classes.
- B. Package names must be unique across all Java applications.
- C. Package names are case-sensitive.
- D. Java does not support packaging.
- **Answer: C. Package names are case-sensitive.**
15. What is the purpose of the `StringBuilder` class in Java?
- A. It is used to build strings with immutable characters.
- B. It is used to build strings with mutable characters.
- C. It is used to split strings into substrings.
- D. It is used to compare strings.
- **Answer: B. It is used to build strings with mutable characters.**
16. Which of the following is a correct way to handle exceptions in Java?
- A. try-catch-finally
- B. try-finally-catch
- C. finally-try-catch
- D. finally-catch-try
- **Answer: A. try-catch-finally**
17. What is the purpose of the `java.lang.Math` class in Java?
- A. It is
used for mathematical operations on primitive data types.
- B. It is used to define mathematical functions.
- C. It is used to convert between different numerical data types.
- D. It is used to handle exceptions related to mathematical operations.
- **Answer: A. It is used for mathematical operations on primitive data types.**
18. Which of the following is true about Java generics?
- A. Generics allow you to specify different data types for different operations.
- B. Generics are only applicable to collections.
- C. Generics are a runtime feature in Java.
- D. Generics are used to improve performance.
- **Answer: A. Generics allow you to specify different data types for different operations.**
19. What does the `extends` keyword represent in Java inheritance?
- A. It represents the ability to implement multiple interfaces.
- B. It represents the ability to override superclass methods.
- C. It represents the ability to inherit members from a superclass.
- D. It represents the ability to hide superclass members.
- **Answer: C. It represents the ability to inherit members from a superclass.**
20. What is the purpose of the `finalize()` method in Java?
- A. It is used to finalize the state of an object before it is garbage collected.
- B. It is used to compare objects for equality.
- C. It is used to invoke the superclass constructor.
- D. It is used to define a method.
- **Answer: A. It is used to finalize the state of an object before it is garbage collected.**
21. Which of the following statements about abstract classes in Java is true?
- A. Abstract classes cannot have constructors.
- B. Abstract classes can be instantiated.
- C. Abstract methods must be implemented in the abstract class itself.
- D. Abstract classes can be final.
- **Answer: A. Abstract classes cannot have constructors.**
22. What is the purpose of the `break` statement in Java?
- A. It is used to exit a loop or switch statement.
- B. It is used to skip the current iteration of a loop.
- C. It is used to return a value from a method.
- D. It is used to throw an exception.
- **Answer: A. It is used to exit a loop or switch statement.**
23. Which of the following is true about Java enums?
- A. Enums can have constructors.
- B. Enums can implement interfaces.
- C. Enums cannot have methods.
- D. Enums cannot have constants.
- **Answer: B. Enums can implement interfaces.**
24. What is the purpose of the `instanceof` operator in Java?
- A. It is used to convert a string to lowercase.
- B. It is used to determine the type of an object.
- C. It is used to check if an object is null.
- D. It is used to concatenate strings.
- **Answer: B. It is used to determine the type of an object.**
25. Which of the following is true about the `Object` class in Java?
- A. All classes in Java are subclasses of the Object class.
- B. The Object class cannot be instantiated.
- C. The Object class does not have any methods.
- D. The Object class cannot be extended.
- **Answer: A. All classes in Java are subclasses of the Object class.**
26. What is the output of the following code snippet?
int x = 5;
System.out.println(x > 2 && x < 10);
- A. true
- B. false
- C. Compilation Error
- D. Runtime Error
- **Answer: A. true**
27. Which of the following is a correct way to create an anonymous inner class in Java?
- A. `MyClass obj = new MyClass() {};`
- B. `MyClass obj = new anonymous MyClass() {};`
- C. `MyClass obj = new MyClass() new {};`
- D. `MyClass obj = new MyClass() => {};`
- **Answer: A. `MyClass obj = new MyClass() {};`**
28. What is the output of the following code snippet?
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[arr.length - 1]);
- A. 1
- B. 4
- C. 5
- D. Compilation Error
- **Answer: C. 5**
29. Which of the following is not a valid type of inner class in Java?
- A. Static inner class
- B. Local inner class
- C. Abstract inner class
- D. Anonymous inner class
- **Answer: C. Abstract inner class**
30. What is the output of the following code snippet?
String str = "hello";
System.out.println(str.substring(2, 4));
- A. he
- B. ll
- C. llo
- D. Compilation Error
- **Answer: C. ll**
31. Which of the following is not a valid way to create a thread in Java?
- A. Extend the `Thread` class and override the `run()` method.
- B. Implement the `Runnable` interface and pass it to a `Thread` constructor.
- C. Use the `start()` method of the `Thread` class.
- D. Use the `run()` method of the `Thread` class.
- **Answer: D. Use the `run()` method of the `Thread` class.**
32. What is the output of the following code snippet?
int x = 5;
System.out.println(x += 3);
- A. 5
- B. 3
- C. 8
- D. 15
- **Answer: C. 8**
33. Which of the following is not a valid modifier in Java?
- A. public
- B. private
- C. final
- D. constant
- **Answer: D. constant**
34. What is the output of the following code snippet?
int x = 5;
System.out.println(x--);
- A. 5
- B. 4
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
35. Which of the following is true about Java
annotations?
- A. Annotations can have constructors.
- B. Annotations can be used to modify the behavior of Java programs at runtime.
- C. Annotations are only applicable to methods.
- D. Annotations are used to define variables.
- **Answer: B. Annotations can be used to modify the behavior of Java programs at runtime.**
36. What is the output of the following code snippet?
int x = 5;
System.out.println(x == 5 ? "Yes" : "No");
- A. Yes
- B. No
- C. true
- D. false
- **Answer: A. Yes**
37. Which of the following is true about Java lambda expressions?
- A. Lambda expressions can contain multiple statements.
- B. Lambda expressions are instances of the `java.lang.Lambda` class.
- C. Lambda expressions cannot capture variables from their enclosing scope.
- D. Lambda expressions are used to create anonymous classes.
- **Answer: A. Lambda expressions can contain multiple statements.**
38. What is the output of the following code snippet?
int[] arr = new int[5];
System.out.println(arr[0]);
- A. 0
- B. null
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 0**
39. Which of the following is true about Java streams?
- A. Streams are used to read and write data to files.
- B. Streams are only applicable to numerical data types.
- C. Streams are used to process collections of objects in a functional-style manner.
- D. Streams are not supported in Java.
- **Answer: C. Streams are used to process collections of objects in a functional-style
manner.**
40. What is the output of the following code snippet?
String str = "hello";
System.out.println(str.charAt(2));
- A. h
- B. e
- C. l
- D. o
- **Answer: C. l**
Advanced Level
1. What is the output of the following code snippet?
int x = 10;
int y = x++ + ++x;
System.out.println(y);
- A. 20
- B. 21
- C. 22
- D. 23
- **Answer: D. 23**
2. What is the purpose of the `assert` statement in Java?
- A. It is used to declare an assertion.
- B. It is used to compare two objects for equality.
- C. It is used to throw an exception.
- D. It is used to check assumptions about the program.
- **Answer: D. It is used to check assumptions about the program.**
3. Which of the following is true about Java reflection?
- A. Reflection is used to generate random numbers.
- B. Reflection is used to inspect and modify class behavior at runtime.
- C. Reflection is used to compare objects for equality.
- D. Reflection is used to perform bitwise operations.
- **Answer: B. Reflection is used to inspect and modify class behavior at runtime.**
4. What is the purpose of the `ThreadLocal` class in Java?
- A. It is used to define a thread.
- B. It is used to create a thread-safe singleton class.
- C. It is used to store data that is specific to a thread.
- D. It is used to terminate a thread.
- **Answer: C. It is used to store data that is specific to a thread.**
5. What is the output of the following code snippet?
String str = "hello";
str.concat(" world");
System.out.println(str);
- A. hello
- B. hello world
- C. world
- D. Compilation Error
- **Answer: A. hello**
6. Which of the following is not a valid type of exception in Java?
- A. Checked
- B. Unchecked
- C. Runtime
- D. Fatal
- **Answer: D. Fatal**
7. What is the purpose of the `strictfp` keyword in Java?
- A. It is used to declare a strict floating-point number.
- B. It is used to enforce strict type checking.
- C. It is used to ensure consistent floating-point arithmetic across different platforms.
- D. It is used to define a strict superclass.
- **Answer: C. It is used to ensure consistent floating-point arithmetic across different
platforms.**
8. What is the output of the following code snippet?
double x = 0.0 / 0.0;
System.out.println(x);
- A. 0.0
- B. 1.0
- C. Infinity
- D. NaN (Not a Number)
- **Answer: D. NaN (Not a Number)**
9. What is the purpose of the `AutoCloseable` interface in Java?
- A. It is used to define a class that can be automatically closed.
- B. It is used to define a class that can be automatically garbage collected.
- C. It is used to define a class that can be automatically serialized.
- D. It is used to define a class that can be automatically instantiated.
- **Answer: A. It is used to define a class that can be automatically closed.**
10. Which of the following is true about Java annotations?
- A. Annotations can have constructors.
- B. Annotations can be used to modify the behavior of Java programs at runtime.
- C. Annotations are only applicable to methods.
- D. Annotations are used to define variables.
- **Answer: B. Annotations can be used to modify the behavior of Java programs at runtime.**
11. What is the output of the following code snippet?
int[] arr = {1, 2, 3, 4, 5};
Arrays.stream(arr).forEach(System.out::print);
- A. 12345
- B. 1 2 3 4 5
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 12345**
12. What is the purpose of the `java.lang.instrument` package in Java?
- A. It is used to manipulate bytecode at runtime.
- B. It is used to perform mathematical operations.
- C. It is used to define instrumented classes.
- D. It is used to handle exceptions related to instrumentation.
- **Answer: A. It is used to manipulate bytecode at runtime.**
13. Which of the following is true about Java serialization?
- A. Serialization is the process of converting an object into a byte stream.
- B. Serialization is only applicable to primitive data types.
- C. Serialization is used to compare objects for equality.
- D. Serialization is used to perform bitwise operations.
- **Answer: A. Serialization is the process of converting an object into a byte stream.**
14. What is the purpose of the `java.util.concurrent` package in Java?
- A. It is used to perform concurrent file I/O operations.
- B. It is used to handle exceptions related to concurrency.
- C. It is used to create concurrent data structures and utilities.
- D. It is used to define concurrent classes.
- **Answer: C. It is used to create concurrent data structures and utilities.**
15. What is the output of the following code snippet?
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
list.replaceAll(i -> i * 2);
System.out.println(list);
- A. [1, 2, 3, 4, 5]
- B. [2, 4, 6, 8, 10]
- C. Compilation Error
- D. Runtime Error
- **Answer: B. [2, 4, 6, 8, 10]**
16. Which of the following is true about Java enums?
- A. Enums can have constructors.
- B. Enums can implement interfaces.
- C. Enums cannot have methods.
- D. Enums cannot have constants.
- **Answer: B.
Enums can implement interfaces.**
17. What is the output of the following code snippet?
int x = 5;
int y = 3;
System.out.println(x > y ? x : y);
- A. 5
- B. 3
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
18. Which of the following is true about Java reflection?
- A. Reflection is used to generate random numbers.
- B. Reflection is used to inspect and modify class behavior at runtime.
- C. Reflection is used to compare objects for equality.
- D. Reflection is used to perform bitwise operations.
- **Answer: B. Reflection is used to inspect and modify class behavior at runtime.**
19. What is the output of the following code snippet?
String str = "hello";
str = str.concat(" world");
System.out.println(str);
- A. hello
- B. hello world
- C. world
- D. Compilation Error
- **Answer: B. hello world**
20. What is the purpose of the `static` keyword when applied to a block of code in Java?
- A. It indicates that the block of code belongs to a subclass.
- B. It indicates that the block of code is executed only once when the class is loaded.
- C. It indicates that the block of code cannot be executed.
- D. It indicates that the block of code can be accessed only by static methods.
- **Answer: B. It indicates that the block of code is executed only once when the class is
loaded.**
21. What is the output of the following code snippet?
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.removeIf(n -> n % 2 == 0);
System.out.println(list);
- A. [1, 2, 3, 4, 5]
- B. [1, 3, 5]
- C. Compilation Error
- D. Runtime Error
- **Answer: B. [1, 3, 5]**
22. Which of the following is true about Java annotations?
- A. Annotations can have constructors.
- B. Annotations can be used to modify the behavior of Java programs at runtime.
- C. Annotations are only applicable to methods.
- D. Annotations are used to define variables.
- **Answer: B. Annotations can be used to modify the behavior of Java programs at runtime.**
23. What is the output of the following code snippet?
String str = "hello";
System.out.println(str.substring(2, 4));
- A. he
- B. ll
- C. llo
- D. Compilation Error
- **Answer: B. ll**
24. Which of the following is true about Java streams?
- A. Streams are used to read and write data to files.
- B. Streams are only applicable to numerical data types.
- C. Streams are used to process collections of objects in a functional-style manner.
- D. Streams are not supported in Java.
- **Answer: C. Streams are used to process collections of objects in a functional-style
manner.**
25. What is the purpose of the `java.util.ServiceLoader` class in Java?
- A. It is used to load service providers at runtime.
- B. It is used to define service providers.
- C. It is used to handle exceptions related to service loading.
- D. It is used to access system services.
- **Answer: A. It is used to load service providers at runtime.**
26. What is the output of the following code snippet?
int x = 5;
System.out.println(x += 3);
- A. 5
- B. 3
- C. 8
- D. 15
- **Answer: C. 8**
27. Which of the following is true about Java generics?
- A. Generics allow you to specify different data types for different operations.
- B. Generics are only applicable to collections.
- C. Generics are a runtime feature in Java.
- D. Generics are used to improve performance.
- **Answer: A. Generics allow you to specify different data types for different operations.**
28. What is the output of the following code snippet?
Map<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.computeIfAbsent(4, key -> "four");
System.out.println(map);
- A. {1=one, 2=two, 3=three, 4=four}
- B. {1=one, 2=two, 3=three}
- C. {4=four}
- D. Compilation Error
- **Answer: A. {1=one, 2=two, 3=three, 4=four}**
29. What is the purpose of the `java.util.Properties` class in Java?
- A. It is used to store system properties.
- B. It is used to define properties files.
- C. It is used to define system properties.
- D. It is used to load properties files.
- **Answer: A. It is used to store system properties.**
30. Which of the following is true about Java lambda expressions?
- A. Lambda expressions can contain multiple statements.
- B. Lambda expressions are instances of the `java.lang.Lambda` class.
- C. Lambda expressions cannot capture variables from their enclosing scope.
- D. Lambda expressions are used to create anonymous classes.
- **Answer: A. Lambda expressions can contain multiple statements.**
31. What is the output of the following code snippet?
int x = 5;
System.out.println(++x * x--);
- A. 25
- B. 20
- C. 30
- D. 35
- **Answer: A. 25**
32. Which of the following is true about Java serialization?
- A. Serialization is the process of converting an object into a byte stream.
- B. Serialization is only applicable to primitive data types.
- C. Serialization is used to compare objects for equality.
- D. Serialization is used to perform bitwise operations.
- **Answer: A. Serialization is the process of converting an object into a byte stream.**
33. What is the output of the following code snippet?
int[] arr = new int[5];
System.out.println(arr[0]);
- A. 0
- B. null
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 0**
34. What is the purpose of the `java.lang.Process` class in Java?
- A. It is used to execute system commands.
- B. It is used to define processes.
- C. It is used to handle exceptions related to processes.
- D. It is used to access system processes.
- **Answer: A. It is used to execute system commands.**
35. Which of the following is true about Java streams?
- A. Streams are used to read and write data to files.
- B. Streams are only applicable to numerical data types.
- C. Streams are used to process collections of objects in a functional-style manner.
- D. Streams are not supported in Java.
- **Answer: C. Streams are used to process collections of objects in a functional-style
manner.**
36. What is the output of the following code snippet?
int x = 5;
System.out.println(x > 2 && x < 10);
- A. true
- B. false
- C. Compilation Error
- D. Runtime Error
- **Answer: A. true**
37. What is the purpose of the `java.net.URL` class in Java?
- A. It is used to define URLs.
- B. It is used to access resources on the internet.
- C. It is used to handle exceptions related to URLs.
- D. It is used to define internet protocols.
- **Answer: B. It is used to access resources on the internet.**
38. What is the output of the following code snippet?
String str = "hello";
System.out.println(str.charAt(2));
- A. h
- B. e
- C. l
- D. o
- **Answer: C. l**
39. What is the purpose of the `java.nio` package in Java?
- A. It is used to perform non-blocking I/O operations.
- B. It is used to define new I/O streams.
- C. It is used to handle exceptions related to I/O operations.
- D. It is used to access system I/O devices.
- **Answer: A. It is used to perform non-blocking I/O operations.**
40. What is the output of the following code snippet?
int x = 5;
System.out.println(x == 5 ? "Yes" : "No");
- A. Yes
- B. No
- C. true
- D. false
- **Answer: A. Yes**
Syntax Based Questions
1. What is the error in the following Java code?
public class MyClass
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- A. Missing semicolon after the class declaration
- B. Missing semicolon after the method declaration
- C. Missing opening brace after the class declaration
- D. Missing closing brace after the method declaration
- **Answer: A. Missing opening brace after the class declaration**
2. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!")
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the string literal
- C. Missing opening parenthesis after the method call
- D. Missing closing parenthesis after the method call
- **Answer: B. Missing semicolon after the string literal**
3. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10
System.out.println(x);
}
}
- A. Missing semicolon after the variable declaration
- B. Missing opening parenthesis after the variable declaration
- C. Missing closing parenthesis after the variable declaration
- D. Missing comma after the variable declaration
- **Answer: A. Missing semicolon after the variable declaration**
4. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x)
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the variable declaration
- C. Missing opening parenthesis after the method call
- D. Missing closing parenthesis after the method call
- **Answer: A. Missing semicolon after the method call**
5. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!")
}
}
- A. Missing semicolon after the string literal
- B. Missing semicolon after the method call
- C. Missing semicolon after the class declaration
- D. No error
- **Answer: A. Missing semicolon after the string literal**
6. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args)
System.out.println("Hello, World!");
}
}
- A. Missing opening brace after the method declaration
- B. Missing closing brace after the method declaration
- C. Missing semicolon after the method declaration
- D. No error
- **Answer: A. Missing opening brace after the method declaration**
7. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println(x + y)
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the variable declaration
- C. Missing operator between variables
- D. Missing closing parenthesis after the method call
- **Answer: A. Missing semicolon after the method call**
8. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
- A. Missing semicolon after the method call
- B. Missing opening brace after the method declaration
- C. Missing closing brace after the method declaration
- D. No error
- **Answer: C. Missing closing brace after the method declaration**
9. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println("Value of x: " x);
}
}
- A. Missing plus operator between string and variable
- B. Missing semicolon after the string literal
- C. Missing opening parenthesis before x
- D. No error
- **Answer: A. Missing plus operator between string and variable**
10. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10
System.out.println(x);
}
}
- A. Missing semicolon after the variable declaration
- B. Missing opening parenthesis before x
- C. Missing closing parenthesis after x
- D. No error
- **Answer: A. Missing semicolon after the variable declaration**
11. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x))
}
}
- A. Missing semicolon after the method call
- B. Missing opening parenthesis before the method call
- C. Missing closing parenthesis after the method call
- D. No error
- **Answer: C. Missing closing parenthesis after the method call**
12. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x 10;
System.out.println(x);
}
}
- A. Missing semicolon after the variable declaration
- B. Missing equal sign between variable and value
- C. Missing opening parenthesis before 10
- D. No error
- **Answer: B. Missing equal sign between variable and value**
13. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println("Value of x: " + x;
}
}
- A. Missing semicolon after the method call
- B. Missing closing parenthesis after x
- C. Missing opening parenthesis before x
- D. No error
- **Answer: B. Missing closing parenthesis after x**
14. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println(x + y);
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the variable declaration
- C. Missing plus operator between variables
- D. No error
- **Answer: D. No error**
15. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!)
}
}
- A. Missing semicolon after the string literal
- B. Missing opening parenthesis before the string literal
- C. Missing closing parenthesis after the string literal
- D. No error
- **Answer: C. Missing closing parenthesis after the string literal**
16. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x)
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the variable declaration
- C. Missing opening parenthesis before the method call
- D. No error
- **Answer: A. Missing semicolon after the method call**
17. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!")
}
- A. Missing semicolon after the string literal
- B. Missing opening brace after the method declaration
- C. Missing closing brace after the method declaration
- D. No error
- **Answer: B. Missing opening brace after the method declaration**
18. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!")
}
}
- A. Missing semicolon after the string literal
- B. Missing opening brace after the class declaration
- C. Missing closing brace after the class declaration
- D. No error
- **Answer: C. Missing closing brace after the class declaration**
19. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println("Value of x: " + x);
}
- A. Missing semicolon after the method call
- B. Missing closing brace after the method declaration
- C. Missing opening brace after the method declaration
- D. No error
- **Answer: C. Missing opening brace after the method declaration**
20. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!")
}
}
- A. Missing semicolon after the string literal
- B. Missing semicolon after the method call
- C. Missing semicolon after the class declaration
- D. No error
- **Answer: A. Missing semicolon after the string literal**
21. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
- A. Missing semicolon after the method call
- B. Missing closing brace after the method declaration
- C. Missing opening brace after the method declaration
- D. No error
- **Answer: C. Missing opening brace after the method declaration**
22. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
- A. Missing semicolon after the string literal
- B. Missing semicolon after the method call
- C. Missing closing brace after the method declaration
- D. No error
- **Answer: C. Missing closing brace after the method declaration**
23. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x)
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the variable declaration
- C. Missing opening brace after the method declaration
- D. No error
- **Answer: A. Missing semicolon after the method call**
24. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println("Value of x: " + x);
}
- A. Missing semicolon after the method call
- B. Missing closing brace after the method declaration
- C. Missing opening brace after the method declaration
- D. No error
- **Answer: C. Missing opening brace after the method declaration**
25. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!")
}
}
- A. Missing semicolon after the string literal
- B. Missing semicolon after the method call
- C. Missing semicolon after the class declaration
- D. No error
- **Answer: A. Missing semicolon after the string literal**
26. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println(x + y)
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the variable declaration
- C. Missing plus operator between variables
- D. No error
- **Answer: A. Missing semicolon after the method call**
27. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!)
}
}
- A. Missing semicolon after the string literal
- B. Missing opening parenthesis before the string literal
- C. Missing closing parenthesis after the string literal
- D. No error
- **Answer: C. Missing closing parenthesis after the string literal**
28. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x))
}
}
- A. Missing semicolon after the method call
- B. Missing opening parenthesis before the method call
- C. Missing closing parenthesis after the method call
- D. No error
- **Answer: C. Missing closing parenthesis after the method call**
29. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x 10;
System.out.println(x);
}
}
- A. Missing semicolon after the variable declaration
- B. Missing equal sign between variable and value
- C. Missing opening parenthesis before 10
- D. No error
- **Answer: B. Missing equal sign between variable and value**
30. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println("Value of x: " + x;
}
}
- A. Missing semicolon after the method call
- B. Missing closing parenthesis after x
- C. Missing opening parenthesis before x
- D. No error
- **Answer: B. Missing closing parenthesis after x**
31. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
- A. Missing semicolon after the method call
- B. Missing closing brace after the method declaration
- C. Missing opening brace after the method declaration
- D. No error
- **Answer: C. Missing opening brace after the method declaration**
32. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
- A. Missing semicolon after the string literal
- B. Missing semicolon after the method call
- C. Missing closing brace after the method declaration
- D. No error
- **Answer: C. Missing closing brace after the method declaration**
33. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x)
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the variable declaration
- C. Missing opening brace after the method declaration
- D. No error
- **Answer: A. Missing semicolon after the method call**
34. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println("Value of x: " + x);
}
- A. Missing semicolon after the method call
- B. Missing closing brace after the method declaration
- C. Missing opening brace after the method declaration
- D. No error
- **Answer: C. Missing opening brace after the method declaration**
35. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!)
}
}
- A. Missing semicolon after the string literal
- B. Missing opening parenthesis before the string literal
- C. Missing closing parenthesis after the string literal
- D. No error
- **Answer: C. Missing closing parenthesis after the string literal**
36. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x))
}
}
- A. Missing semicolon after the method call
- B. Missing opening parenthesis before the method call
- C. Missing closing parenthesis after the method call
- D. No error
- **Answer: C. Missing closing parenthesis after the method call**
37. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x 10;
System.out.println(x);
}
}
- A. Missing semicolon after the variable declaration
- B. Missing equal sign between variable and value
- C. Missing opening parenthesis before 10
- D. No error
- **Answer: B. Missing equal sign between variable and value**
38. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println("Value of x: " + x;
}
}
- A. Missing semicolon after the method call
- B. Missing closing parenthesis after x
- C. Missing opening parenthesis before x
- D. No error
- **Answer: B. Missing closing parenthesis after x**
39. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println(x + y)
}
}
- A. Missing semicolon after the method call
- B. Missing semicolon after the variable declaration
- C. Missing plus operator between variables
- D. No error
- **Answer: A. Missing semicolon after the method call**
40. What is the error in the following Java code?
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World!)
}
}
- A. Missing semicolon after the string literal
- B. Missing opening parenthesis before the string literal
- C. Missing closing parenthesis after the string literal
- D. No error
- **Answer: C. Missing closing parenthesis after the string literal**