0% found this document useful (0 votes)
2 views13 pages

Java_MCQs

The document contains multiple-choice questions (MCQs) related to Java programming, covering topics such as data types, class creation, exception handling, and collection classes. Each section consists of ten questions, with options provided for answers. The questions test knowledge on Java syntax, functionality, and core concepts.

Uploaded by

Dipali Chormale
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)
2 views13 pages

Java_MCQs

The document contains multiple-choice questions (MCQs) related to Java programming, covering topics such as data types, class creation, exception handling, and collection classes. Each section consists of ten questions, with options provided for answers. The questions test knowledge on Java syntax, functionality, and core concepts.

Uploaded by

Dipali Chormale
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/ 13

Java MCQs

Section 1: (Q1-Q10)

Q1. Which of the following is not a primitive data type


in Java?
A. int
B. String
C. float
D. Boolean
Q2. Which keyword is used to create a class in Java?
A. object
B. define
C. class
D. new
Q3. What is the default value of a boolean variable in
Java?
A. true
B. false
C. 0
D. null
Q4. Which of the following is a valid array declaration in
Java?
A. int arr[] = new int(5);
B. int arr[5];
C. int[] arr = new int[5];
D. array arr = int[5];
Q5. Which method is used to compare two strings in
Java for content?
A. ==
B. equals()
C. compareTo()
D. equalsIgnoreCase()
Q6. Which loop is guaranteed to execute at least once?
A. for
B. while
C. do-while
D. None.
Q7. What is the output of System.out.println(4 + 4 +
"Java");?
A.Java44
B. 8Java
C. Java8
D. Compilation error
Q8. Which access modifier allows visibility within the
same package?
A. private
B. public
C. protected
D. (default/no modifier)
Q9. System.out.println() — the out is an object of which
class?
A. System
B. OutputStream
C. PrintStream
D. Console.
Q10. Which class does the println() method belong to?
A. System
B. PrintWriter
C. PrintStream
D. Object
Section 2: Java MCQs (Q11–Q20)

Q11. Which class is the superclass of all classes in Java?


A. Class
B. Object
C. System
D. Main
Q12. Which of the following keywords is used to inherit
a class?
A. this
B. super
C. extends
D. implements
Q13. Which exception is thrown when dividing by zero
in Java?
A. NullPointerException
B. ArithmeticException
C. IOException
D. ClassNotFoundException
Q14. Which class is used to create a thread by extending
it?
A. Runnable
B. Thread
C. Executor
D. ThreadPool
Q15. What is the use of finally block?
A. Executes only when exception occurs
B. Executes only when no exception
C. Executes always
D. Used for returning data
Q16. Which method must be implemented when using
the Runnable interface?
A. start()
B. run()
C. execute()
D. init()
Q17. Which operator is used to create an object in Java?
A. malloc
B. new
C. alloc
D. create
Q18. Which package contains the Scanner class?
A. java.io
B. java.net
C. java.util
D. java.lang
Q19. Which exception is thrown when accessing an
array with an illegal index?
A. ArrayIndexOutOfBoundsException
B. IndexException
C. IllegalAccessException
D. OutOfMemoryError
Q20. Which collection class does not allow duplicate
elements?
A. ArrayList
B. HashSet
C. LinkedList
D. Vector

Section 3: Java MCQs (Q21–Q30)


Q21.
What will be the output of the following code?
java
CopyEdit
int x = 5;
int y = ++x * 2;
System.out.println(y);
A. 10
B. 12
C. 11
D. 14

Q22.
What will be the result of the following code?
java
CopyEdit
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.remove("a");
System.out.println(list);
A. [a, b]
B. [b]
C. [a]
D. []

Q23.
What is the output of the following code?
java
CopyEdit
String str = "Java";
System.out.println(str.charAt(2));
A. J
B. a
C. v
D. Compilation error

Q24.
Choose the correct output of the following code:
java
CopyEdit
int[] arr = {1, 2, 3};
System.out.println(arr.length);
A. 2
B. 3
C. 4
D. Compilation Error

Q25.
What does the following code print?
java
CopyEdit
String s = "abc";
s.concat("def");
System.out.println(s);
A. abc
B. abcdef
C. def
D. Compilation error
Q36.
Choose the correct output:
java
CopyEdit
int x = 5;
int y = x++ + ++x;
System.out.println(y);
A. 10
B. 11
C. 12
D. 13

Q27.
What is the output of the following code?
java
CopyEdit
Map<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(1, "B");
System.out.println(map.get(1));
A. A
B. B
C. null
D. 1

Q28.
What is the result of the following code execution?
java
CopyEdit
Thread t = new Thread(() ->
System.out.print("Running"));
t.start();
A. Running
B. Error
C. Thread
D. Compilation Error
Q29.
What happens when this serialization code is executed
on a class not implementing Serializable?
java
CopyEdit
ObjectOutputStream out = new
ObjectOutputStream(new
FileOutputStream("data.txt"));
out.writeObject(new MyClass());
A. File is created with data
B. No error, but nothing is written
C. NotSerializableException is thrown
D. IOException is thrown

Q30.
What is the output of the following collection code?
java
CopyEdit
Set<String> set = new HashSet<>();
set.add("one");
set.add("two");
set.add("one");
System.out.println(set.size());
A. 0
B. 1
C. 3
D. 2

You might also like