Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Java MCQs With Answers Set 2

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)
9 views7 pages

Java MCQs With Answers Set 2

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
You are on page 1/ 7

Java MCQs with Answers - Set 2

1. What is the output of the following code?


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

A) 11
B) 12
C) 10
D) 13
Answer: A) 11

2. Which of the following is true about volatile keyword?

A) It guarantees atomicity
B) It guarantees visibility of changes across threads
C) It can only be used with methods
D) It prevents JVM from reordering instructions
Answer: B) It guarantees visibility of changes across threads

3. What will be the output?


java
CopyEdit
String s1 = "Hello";
String s2 = new String("Hello");
System.out.println(s1 == s2);

A) true
B) false
C) Compilation error
D) Runtime error
Answer: B) false
4. Which exception is thrown when a thread is waiting, sleeping or otherwise occupied, and
the thread is interrupted?

A) InterruptedException
B) IllegalThreadStateException
C) ThreadDeath
D) IOException
Answer: A) InterruptedException

5. Which method is used to prevent serialization?

A) readResolve()
B) writeObject()
C) private Object readObject()
D) private Object writeReplace()
Answer: D) private Object writeReplace()

6. What does the transient keyword do?

A) Prevents method from being overridden


B) Makes variable synchronized
C) Prevents serialization of a variable
D) Marks variable as volatile
Answer: C) Prevents serialization of a variable

7. Which of these collections guarantees thread safety?

A) HashMap
B) LinkedList
C) Hashtable
D) TreeMap
Answer: C) Hashtable

8. Which is not a valid functional interface in Java 8?

A) Supplier
B) Predicate
C) Comparator
D) Map
Answer: D) Map

9. Which design pattern is used in java.lang.Runtime?

A) Factory
B) Singleton
C) Builder
D) Adapter
Answer: B) Singleton

10. What is the result of:


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

A) 30304050
B) 303090
C) 303050
D) 303040
Answer: C) 303050

11. Which stream is used to serialize objects in Java?

A) ObjectReader
B) DataInputStream
C) ObjectOutputStream
D) SerializableStream
Answer: C) ObjectOutputStream

12. What is the result of:


java
CopyEdit
System.out.println("one".concat("two") == "onetwo");

A) true
B) false
C) Compilation error
D) Runtime error
Answer: B) false
13. In Java 8, which of the following supports lazy evaluation?

A) List
B) Stream
C) Optional
D) Future
Answer: B) Stream

14. What will be the output of this code?


java
CopyEdit
List<String> list = Arrays.asList("a", "b", "c");
list.replaceAll(String::toUpperCase);
System.out.println(list);

A) [A, B, C]
B) [a, b, c]
C) Compilation Error
D) Runtime Error
Answer: A) [A, B, C]

15. Which method cannot be overridden?

A) static
B) final
C) private
D) All of the above
Answer: D) All of the above

16. Which functional interface returns a boolean?

A) Predicate
B) Supplier
C) Consumer
D) Function
Answer: A) Predicate
17. Which is not part of Java Memory Model?

A) Stack
B) Heap
C) PermGen
D) BIOS
Answer: D) BIOS

18. Which stream class is used to write primitive data?

A) BufferedWriter
B) DataOutputStream
C) ObjectOutputStream
D) FileWriter
Answer: B) DataOutputStream

19. What is the type of lambda expression (a, b) -> a + b?

A) Predicate
B) BiFunction
C) Supplier
D) Runnable
Answer: B) BiFunction

20. Which of the following classes is immutable?

A) StringBuilder
B) StringBuffer
C) String
D) Date
Answer: C) String

21. Which of these is true about Java enums?

A) Enums can extend another class


B) Enums are reference types
C) Enums cannot have methods
D) Enums can't implement interfaces
Answer: B) Enums are reference types
22. What is the output?
java
CopyEdit
Integer a = 128, b = 128;
System.out.println(a == b);

A) true
B) false
C) Compilation Error
D) Exception
Answer: B) false

23. What is the output?


java
CopyEdit
Integer a = 127, b = 127;
System.out.println(a == b);

A) true
B) false
Answer: A) true (due to Integer caching from -128 to 127)

24. Which interface provides ability to iterate in reverse?

A) Iterator
B) Enumeration
C) ListIterator
D) Iterable
Answer: C) ListIterator

25. What does default keyword in an interface method mean?

A) It must be overridden
B) It is abstract
C) It has a default implementation
D) It is static
Answer: C) It has a default implementation
26. What does Optional.orElseThrow() do?

A) Returns value if present


B) Returns null if not present
C) Throws exception if value not present
D) Does nothing
Answer: C) Throws exception if value not present

27. What is the type of (x) -> x * x?

A) Consumer
B) Function
C) Predicate
D) BiConsumer
Answer: B) Function

28. Which one is not a valid access modifier in Java?

A) public
B) private
C) protected
D) friendly
Answer: D) friendly

29. What happens if you try to override a static method?


A) Method is overridden
B) Method is hidden, not overridden
C) Compile-time error
D) Runtime exception
Answer: B) Method is hidden, not overridden

30. Which Java feature allows passing behavior as a parameter?


A) Inheritance
B) Encapsulation
C) Functional interfaces
D) Polymorphism
Answer: C) Functional interfaces

You might also like