Java MCQ.docx
Java MCQ.docx
Java Introduction:
o A) .java
o B) .class
o C) .exe
o D) .jar
Answer: B
o A) Object-oriented
o B) Platform-dependent
o C) Multithreaded
o D) Architecture-neutral
Answer: B
o A) main()
o B) start()
o C) run()
o D) init()
Answer: A
o A) System.out.println("Hello World");
o B) println("Hello World");
o C) echo("Hello World");
o D) Console.log("Hello World");
Answer: A
o A) JVM
o B) JDK
o C) JRE
o D) Both A and C
Answer: D
● A) JDK
● B) JVM
● C) JRE
Java Variables:
● A) int number;
● B) int 1number;
● C) int #number;
● D) int number@;
Answer: A
● A) 0
● B) Null
● C) Garbage value
● D) -1
Answer: A
● A) int
● B) String
● C) float
● D) char
Answer: B
● A) 2 bytes
● B) 4 bytes
● C) 8 bytes
Java Operators:
● A) +
● B) -
● C) &
● D) %
Answer: C
● A) 21
● B) 11
● C) 13
● D) 17
Answer: B
● A) System
● B) Scanner
● C) InputStream
● D) BufferedReader
Answer: B
21. Which of the following is the correct way to read a string from the user?
● C) Both A and B
● B) A block of code
● C) A single statement
● C) A condition statement
Java Comments:
● A) /* Comment */
● B) # Comment
● C) // Comment
● A) // Comment
● B) /* Comment */
● C) ### Comment
● D) !! Comment
Answer: B
● A) if-else
● B) switch
● C) for
● D) Both A and B
Answer: D
Java if...else:
java
Copy code
if (5 > 2) {
System.out.println("True");
} else {
System.out.println("False");
● A) True
● B) False
● C) Error
● C) To terminate a loop
● A) It is mandatory
● B) It is executed if no matching case is found
● C) Both A and B
● A) while (condition) { }
● B) while condition { }
● C) while: condition { }
● D) while(condition);
Answer: A
● A) It skips the current iteration and continues with the next one
● D) It throws an exception
Answer: A
Additional Questions:
● A) class
● B) try
● C) new
● D) include
Answer: D
41. Which method is used to take integer input from the user in Java?
● A) next()
● B) nextInt()
● C) inputInt()
● D) readInt()
Answer: B
42. Which of the following keywords is used to define a constant variable in Java?
● A) const
● B) final
● C) static
● A) float
● B) double
● C) string
● D) boolean
Answer: C
● A) for loop
● B) while loop
● C) do-while loop
● D) None of the above
Answer: C
● A) for
● B) while
● C) loop
● D) do-while
Answer: C
● A) =
● B) ==
● C) !=
● D) <>
Answer: B
● A) break
● B) continue
● C) return
● D) exit
Answer: A
o a) JDK
o b) JVM
o c) JRE
o d) Bytecode
Answer: b
o a) JRE
o b) JVM
o c) JDK
o a) Object-oriented programming
o b) Structured programming
o c) Pointers
o a) C
o b) C++
o c) Both
o d) None
Answer: b
o a) int
o b) float
o c) double
o d) Both b and c
Answer: d
o a) 1 byte
o b) 2 bytes
o c) 4 bytes
o d) Platform-dependent
Answer: b
o a) final
o b) const
o c) static
o d) None
Answer: a
Java Operators
o a) %
o b) //
o c) &&
o d) None
Answer: a
o a) 21
o b) 11
o c) 17
o d) None
Answer: b
14. What is the result of the expression true && false in Java?
o a) true
o b) false
o c) Compilation error
o d) None
Answer: b
o a) nextLine()
o b) readLine()
o c) readline()
o d) None
Answer: a
o c) Throws an exception
o a) Compiler
o b) JVM
o c) API libraries
o d) Operating System
Answer: d
o a) javac
o b) java
o c) compile
o b) C++
o d) None
Answer: b
o a) int
o b) boolean
o c) String
o d) float
Answer: c
o a) 0
o b) null
o d) No default value
Answer: d
o a) 128
o b) 255
o c) 127
Java Operators
o a) 1
o b) -1
o c) 0
o d) None of the above
Answer: a
o a) &
o b) +
o c) *
12. Which method is used to read an integer input in Java using the Scanner class?
o a) next()
o b) nextInt()
o c) nextInteger()
o d) readInt()
Answer: b
o a) InputStream
o b) OutputStream
o c) System.in
o d) System.out
Answer: d
o a) scanner.close();
o b) scanner.end();
o c) scanner.quit();
java
Copy code
System.out.print("Hello ");
System.out.println("World!");
o a) HelloWorld!
o b) Hello World!
o c) Hello
World!
o d) Compilation error
Answer: b
java
Copy code
● a) 51
● b) 52
● c) 50
o a) 1 byte
o b) 2 bytes
o c) 4 bytes
o d) 8 bytes
Answer: b
o a) float f = 3.14;
o b) float f = 3.14f;
o c) float f = 3;
o d) Both b and c
Answer: d
o a) Compile-time error
2. Java Operators
java
Copy code
o a) 51
o b) 52
o c) 50
o a) &
o b) &&
o c) |
o d) ~
Answer: a
o a) Compile-time error
o b) "hellonull"
o c) Runtime Exception
o a) BufferedReader is faster.
o b) Scanner is faster.
o c) Both are equally fast.
java
Copy code
int x = 5;
int x = 10;
System.out.println(x);
o a) 5
o b) 10
o c) Compile-time error
o d) Runtime error
Answer: c
5. Java if-else
java
Copy code
int x = 5;
if (x == 5) if (x >= 5) System.out.println("A"); else System.out.println("B");
o a) A
o b) B
o c) Compile-time error
o a) Yes
o b) No
o c) Nothing happens.
o a) A collection or array
o b) A defined index
o c) A continue statement
a) JVM + Libraries
d) Only JVM
UNIT : 2
a) new
b) class
c) object
d) this
5. Which of the following is true about the 'this' keyword in Java? a) It refers to the current
object.
b) It is used to refer to static members.
c) It can be used to refer to class constructors only.
d) It is used for exception handling.
a) static
b) void
c) class
d) package
7. Which access modifier provides the most restricted access in Java?- a) private
b) protected
c) public
d) default
9. What does recursion refer to in Java? -a) A method calling another method
b) A method calling itself
c) A method inside a class
d) Multiple methods in a single class
10. What is the use of the instanceof operator in Java? a) To compare two objects
b) To check if an object belongs to a specific class or subclass
c) To declare an object
d) To delete an object
11. Which of these is correct syntax for creating an object of class A? a) A obj = A();
b) A obj = new A();
c) A obj;
d) new A obj;
12. Which type of constructor does Java provide if no constructor is defined?- a) Parameterized
constructor
b) Default constructor
c) Copy constructor
d) Abstract constructor
13. Which of these defines a string object correctly in Java? a) String s = "Hello";
b) string s = "Hello";
c) char s = "Hello";
d) String s = new String();
15. If a class is declared as final, what cannot be done? a) The class cannot inherit from another
class.
b) The class cannot be instantiated.
c) The class cannot be subclassed.
d) The class cannot contain variables.
16. What happens if a method has the same name as the class in Java? a) It is treated as a
constructor.
b) It is an invalid method.
c) It results in an error.
d) The program compiles but gives a runtime error.
17. Which of these statements is correct about static methods in Java? a) Static methods can
access instance variables.
b) Static methods cannot access instance variables.
c) Static methods are created at runtime.
d) Static methods must return a value.
18. What is the keyword this mainly used for? -a) To call the current class method
b) To refer to the current object
c) To access static variables
d) To create an instance of an object
19. Which method in Java is used to check the length of a string? a) size()
b) length()
c) getSize()
d) len()
20. What is inheritance in Java? a) Mechanism by which one class acquires the properties of
another
b) Creating a new class from an existing class
c) Both a and b
d) None of the above
22. Which keyword is used to access a method in a superclass from a subclass? a) this
b) super
c) extends
d) parent
23. What is polymorphism in Java? a) The ability of an object to take many forms
b) The process of creating multiple objects
c) The ability to execute many tasks
d) None of the above
24. Which of these is true for abstract methods? -a) They must have a body.
b) They are declared in a subclass.
c) They must be declared in abstract classes.
d) None of the above
25. How do you implement multiple inheritance in Java? a) Using multiple classes
b) Using interfaces
c) Using multiple constructors
d) Using method overloading
27. What is encapsulation in Java? a) Bundling of data and methods operating on the data
b) Protecting data from unauthorized access
c) Hiding the implementation details of a class
d) All of the above
30. Which statement is true regarding method overloading?- a) Methods must have different
names.
b) Methods must have different parameter lists.
c) Methods must return different types.
d) Methods must be in different classes.
32. What is a static nested class? -a) A nested class that is declared as static
b) A class inside a static block
c) A class that cannot be instantiated
d) A class inside an interface
33. What is the primary purpose of an anonymous class in Java? a) To define a class and
instantiate it in a single step
b) To declare a method without providing a body
c) To hide class names
d) To declare multiple classes at once
35. Which of the following describes an enum in Java? -a) A special type used to define
collections of constants
b) A type of interface
c) A class that cannot be instantiated
d) None of the above
36. Which of the following is true about an enum in Java? a) Enum cannot have constructors.
b) Enum can have fields, constructors, and methods.
c) Enum cannot implement interfaces.
d) Enum can only be used in loops.
37. How do you access a constant in an enum? -a) Using the 'new' keyword
b) Using a static method
c) Using dot notation
d) Using the enum keyword
38. Which of these is NOT a characteristic of a singleton class? a) Only one instance of the class
can exist.
b) It has a private constructor.
c) It must implement an interface.
d) It has a global point of access.
39. What is the return type of an enum’s values() method? -a) Array
b) List
c) String
d) Object
41. What is the difference between inner class and static nested class? a) Inner class can access
outer class members, while static nested class cannot.
1. d) Compilation
2. a) new
3. c) No return type
4. a) null
6. b) void
7. a) private
14. a) equals()
19. b) length()
22. b) super
26. d) Method-hiding
28. a) Yes
39. a) Array
40. a) compareTo()
41. a) Inner class can access outer class members, while static nested class cannot.
Java Strings
java
Copy code
String s1 = "abc";
System.out.println(s1 == s2);
o a) true
o b) false
o c) Compilation error
o d) Runtime exception
Answer: b
o a) compareTo()
o b) equals()
o c) ==
o a) Yes
o b) No
o c) Both a and b.
o a) Compilation error
o d) Runtime error
Answer: b
● a) Methods in a subclass have the same name as in the superclass with the same signature.
java
Copy code
class Outer {
● c) Both a and b
● d) None of the above
Answer: b
Java Enum
14. What happens if two constants in an enum share the same value?
● a) Compilation error
● b) Runtime exception
Java Recursion
java
Copy code
if (n == 0) {
return 1;
System.out.println(factorial(5));
o a) 120
o b) 24
o c) Compilation error
o d) Runtime exception
Answer: a
o a) Fibonacci sequence
o b) Sorting algorithms
o c) Tree traversals
Java Interfaces
java
Copy code
interface A {
void display();
obj.display();
o a) Interface Method
o b) Compilation error
o c) Runtime exception
8. What happens if a class implements two interfaces with the same default method?
o a) Compilation error
java
Copy code
System.out.println(str1 == str2);
System.out.println(str1 == str3);
o a) true, true
o b) true, false
o c) false, false
o d) false, true
Answer: b
o a) StringBuffer
o b) StringBuilder
o c) String
7. Can a static nested class access non-static members of the enclosing class?
o a) Yes
o b) No
11. Can we call a constructor explicitly inside another constructor in the same class?
a) No
String s1 = "abc";
System.out.println(s1 == s2);
a) true
b) false
c) Compilation error
d) Runtime exception
class Test {
int x = 5;
obj1.x = 10;
System.out.println(obj2.x);
a) 5
b) 10
c) Compile-time error
d) Runtime exception
c) Both a and b.
c) Both a and b.
3. What will happen if a class in a package does not have a public modifier?
c) Compilation error.
11. How many catch blocks can be associated with one try block?
a) Only one
b) One or more
c) Exactly two
d) None
12. What happens if an exception is thrown but not caught in Java?
a) The program continues executing
b) The program terminates abruptly
c) The exception is ignored
d) The exception is logged but execution continues
13. Which block is always executed after the try-catch block?-
a) throw
b) throws
c) finally
d) None
14. Can the finally block be skipped in exception handling?
a) Yes, if an exception occurs
b) No, the finally block is always executed
c) Yes, if there is no catch block
d) No, unless the program terminates
15. Which of the following methods can be used to handle multiple exceptions in a
single catch block?-
a) Chaining catch blocks
b) Using multiple exceptions in a single catch block with |
c) Defining custom exception types
d) Using throws with multiple exceptions
16. What happens when a throw statement is executed inside a method?-
a) It immediately returns to the calling method
b) It causes the program to crash
c) It creates and throws a new exception
d) It rethrows an existing exception
17. Which of the following exceptions must be handled or declared in Java?
a) NullPointerException
b) RuntimeException
c) ClassNotFoundException
d) ArrayIndexOutOfBoundsException
11. How many catch blocks can be associated with one try block?
b) One or more
12. What happens if an exception is thrown but not caught in Java?
b) The program terminates abruptly
13. Which block is always executed after the try-catch block?
c) finally
14. Can the finally block be skipped in exception handling?
b) No, the finally block is always executed
(Unless the JVM shuts down due to System.exit() or a fatal error.)
15. Which of the following methods can be used to handle multiple exceptions in a
single catch block?
b) Using multiple exceptions in a single catch block with |
16. What happens when a throw statement is executed inside a method?
c) It creates and throws a new exception
17. Which of the following exceptions must be handled or declared in Java?
c) ClassNotFoundException
java
Copy code
try {
} catch (Exception e) {
System.out.println("Caught Exception");
Java Annotations
Java Multithreading
java
Copy code
______ packageName;
oa) package
ob) import
oc) static
od) None of the above
Answer: a
23. How do you compile a Java program with a package?
o a) javac -d . ClassName.java
o b) javac -d packageName ClassName.java
o c) java ClassName
o d) None of the above
Answer: a
a) @Override
b) @Deprecated
c) @FunctionalInterface
a) New
b) Blocked
c) Running
d) Terminated
UNIT 4 :
String Classes and Operations
java
Copy code
str.concat(" Programming");
System.out.println(str);
o a) Java Programming
o b) Java
o c) Compilation error
o d) Runtime exception
Answer: b
o a) compareTo()
o b) equals()
o c) equalsIgnoreCase()
o d) toLowerCase()
Answer: c
Arrays
java
Copy code
System.out.println(arr[5]);
o a) 5
o b) Compilation error
o c) Runtime exception
Collections Framework
o a) deleteAll()
o b) clear()
o c) removeAll()
o a) Random access
I/O Streams
o a) It throws an exception.
java
Copy code
System.out.println(file.isDirectory());
o a) true
o b) false
o c) Compilation error
o d) Runtime exception
Answer: b
o a) FileOutputStream
o b) PrintWriter
o c) DataOutputStream
o d) BufferedOutputStream
Answer: b
a) List
b) Collection
c) Iterable
d) Map
a) 10
b) 16
a) FileReader
b) FileInputStream
c) BufferedReader
d) PrintWriter
a) ObjectOutputStream
b) DataOutputStream
c) PrintWriter
d) FileWriter
System.out.println(str.substring(1, 4));
a) abc
b) bcd
c) abcd
d) Runtime exception
c) It extends the Collection interface and does not allow duplicate elements.
o a) Driver.load()
o b) Class.forName()
o c) DriverManager.registerDriver()
o a) Type-1
o b) Type-2
o c) Type-3
o d) Type-4
Answer: d
o a) executeQuery()
o b) executeStoredProcedure()
o c) CallableStatement.execute()
o d) executeProcedure()
Answer: c
6. What is the default transaction mode in JDBC?
o a) Auto-commit enabled
o b) Auto-commit disabled
o a) DatabaseMetaData
o b) ResultSetMetaData
o c) ConnectionMetaData
o a) Persistence context
o c) Hibernate context
o d) Persistence unit
Answer: c
o a) save()
o b) persist()
o c) store()
o d) create()
Answer: b
13. What is the default fetch type for @ManyToOne relationships in JPA?
o a) EAGER
o b) LAZY
o c) FETCHED
o a) @NamedQuery
o b) @Query
o c) @PersistentQuery
o a) get()
o b) find()
o c) getSingleResult()
o d) load()
Answer: c
16. What is the main purpose of the Java Persistence API (JPA)?
17. a) @Table
b) @Entity
c) @PersistenceUnit
a) @OneToOne
b) @ManyToMany
c) @PrimaryToSecondary
d) @ManyToOne
a) Hibernate
b) EclipseLink
c) MyBatis
d) OpenJPA
21.
o a) Hibernate
o b) EclipseLink
o c) MyBatis
o d) OpenJPA
Answer: c
o a) Define a relationship.
o a) application.properties
o b) orm.xml
o c) persistence.xml
o d) config.xml
Answer: c