0% found this document useful (0 votes)
58 views7 pages

Java MCQs With Answers Set 1

Uploaded by

Thiyagarajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views7 pages

Java MCQs With Answers Set 1

Uploaded by

Thiyagarajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java MCQs with Answers - Set 1

1. What is the result of the expression: 5 + 2 * 3 - 1 << 2?


A) 20
B) 30
✅ C) 44
D) 16

2. Which design pattern ensures a class has only one instance?


A) Factory
✅ B) Singleton
C) Builder
D) Prototype

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

java
CopyEdit
int x = 5;
int y = ++x * 2 + x--;
System.out.println(y);

A) 10
B) 11
✅ C) 18
D) 20

4. What is the output of this code?

java
CopyEdit
String str1 = "Java";
String str2 = new String("Java");
System.out.println(str1 == str2);

A) true
✅ B) false
C) Compilation Error
D) Runtime Error
5. Which keyword is used to restrict a class from being subclassed?
A) static
✅ B) final
C) abstract
D) protected

6. What does the following code print?

java
CopyEdit
System.out.println(10 + 20 + "30" + 40 + 50);

A) 30304050
✅ B) 30304050
C) 304050
D) 1020304050

7. What will be the output of this code?

java
CopyEdit
public class Test {
public static void main(String[] args) {
int a = 5;
int b = a++ + ++a;
System.out.println(b);
}
}

A) 11
B) 12
✅ C) 13
D) 14

8. What does volatile keyword ensure in Java?


A) Thread-safety
✅ B) Visibility of changes across threads
C) Atomicity
D) Serialization
9. What will the following program output?

java
CopyEdit
public class Test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "a" + "bc";
System.out.println(s1 == s2);
}
}

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

10. Which stream is used for reading primitive data types in Java?
✅ A) DataInputStream
B) BufferedReader
C) InputStreamReader
D) FileInputStream

11. Which exception is thrown when accessing a null object?


A) ClassCastException
B) IOException
✅ C) NullPointerException
D) ArithmeticException

12. What is the output of this program?

java
CopyEdit
public class Main {
public static void main(String[] args) {
int[] arr = new int[5];
System.out.println(arr[0]);
}
}

✅ A) 0
B) null
C) Error
D) garbage value

13. What is the time complexity of HashMap’s get() in average case?


✅ A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)

14. What will happen if a class does not override hashCode() but overrides
equals()?
A) Compilation error
✅ B) Hash-based collections may not work properly
C) Works fine
D) Runtime exception

15. Which annotation is used to override a method in Java?


✅ A) @Override
B) @Overload
C) @Rewrite
D) @Replace

16. What is the result of the following code?

java
CopyEdit
String s = "abc";
s.concat("def");
System.out.println(s);

A) abcdef
✅ B) abc
C) def
D) Compilation Error

17. What is the output of the following?

java
CopyEdit
Integer i1 = 100;
Integer i2 = 100;
System.out.println(i1 == i2);

✅ A) true
B) false
C) Compilation Error
D) Runtime Error

18. What is true about abstract classes?


A) Cannot have constructors
B) Cannot have any method body
C) Can be instantiated
✅ D) Can have both abstract and non-abstract methods

19. Which of these can cause a deadlock?


A) Using final methods
B) Using volatile variables
C) Using static blocks
✅ D) Nested synchronized blocks

20. What does this code output?

java
CopyEdit
String a = "hello";
String b = "he" + "llo";
System.out.println(a == b);

✅ A) true
B) false
C) Error
D) null

21. Which concept allows multiple methods with same name but different
parameters?
✅ A) Overloading
B) Overriding
C) Inheritance
D) Encapsulation

22. Which interface handles comparison of objects?


A) Comparator
B) Clonable
✅ C) Comparable
D) Iterable

23. What is the output of this code?

java
CopyEdit
int a = 10;
int b = 20;
System.out.println(a+++b);

A) 30
✅ B) 31
C) 11
D) Compilation Error

24. What will happen when main() is declared private?


A) Compiles and runs
B) Compilation error
✅ C) Runtime error
D) Executes without output

25. Which is used to perform cleanup before garbage collection?


A) dispose()
✅ B) finalize()
C) destructor()
D) terminate()

26. Which collection is best for LIFO operations?


A) Queue
✅ B) Stack
C) ArrayList
D) LinkedList

27. Which keyword is used for exception handling?


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

28. Which of the following classes is immutable?


✅ A) String
B) StringBuilder
C) ArrayList
D) HashMap

29. Which of these classes implements the Map interface?


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

30. Which access specifier makes variables visible only within the same package?
A) private
B) protected
C) public
✅ D) default

You might also like