0% found this document useful (0 votes)
19 views

Java MCQ.docx

Uploaded by

arunima kushwaha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java MCQ.docx

Uploaded by

arunima kushwaha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

UNIT : 1

Java Introduction:

1. Which of the following is true about Java?

o A) Java is a compiled language

o B) Java is an interpreted language

o C) Java is both compiled and interpreted

o D) None of the above


Answer: C

2. What is the extension of a compiled Java class file?

o A) .java

o B) .class

o C) .exe

o D) .jar
Answer: B

3. Which of the following is not a feature of Java?

o A) Object-oriented

o B) Platform-dependent

o C) Multithreaded

o D) Architecture-neutral
Answer: B

4. What is bytecode in Java? -

o A) The code generated after compilation

o B) Code written by the programmer

o C) Code that runs inside the browser

o D) None of the above


Answer: A

5. What is JVM in Java? -

o A) Java Visual Machine

o B) Java Variable Machine

o C) Java Virtual Machine


o D) Java Visible Machine
Answer: C

Java Hello World:

6. Which method is the entry point of any Java program?-

o A) main()

o B) start()

o C) run()

o D) init()
Answer: A

7. What is the correct syntax to print a message in Java?-

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

8. In which of the following environments can a Java program run?

o A) JVM

o B) JDK

o C) JRE

o D) Both A and C
Answer: D

Java JVM, JRE, and JDK:

9. What does JDK stand for? -

o A) Java Development Kit

o B) Java Deployment Kit

o C) Java Development Kernel

o D) Java Deployment Kernel


Answer: A

10. What is JRE in Java? -

● A) Java Runtime Environment

● B) Java Runtime Executor

● C) Java Run Extension


● D) Java Read Environment
Answer: A

11. Which of the following contains the Java compiler?

● A) JDK

● B) JVM

● C) JRE

● D) None of the above


Answer: A

Difference between C and C++ and Java:

12. Which of the following is a difference between Java and C++?

● A) Java supports multiple inheritance, C++ does not

● B) Java has a pointer type, C++ does not

● C) Java has automatic garbage collection, C++ does not

● D) Java uses a preprocessor, C++ does not


Answer: C

13. Which of the following is a similarity between C++ and Java? -

● A) Both support operator overloading

● B) Both are object-oriented languages

● C) Both support multiple inheritance through classes

● D) Both are platform-dependent


Answer: B

Java Variables:

14. Which of the following is a valid variable declaration in Java? -

● A) int number;

● B) int 1number;

● C) int #number;

● D) int number@;
Answer: A

15. What is the default value of an uninitialized int variable in Java?-

● A) 0

● B) Null

● C) Garbage value
● D) -1
Answer: A

Java Data Types:

16. Which of the following is not a primitive data type in Java?

● A) int

● B) String

● C) float

● D) char
Answer: B

17. What is the size of an int data type in Java?-

● A) 2 bytes

● B) 4 bytes

● C) 8 bytes

● D) Depends on the system


Answer: B

Java Operators:

18. Which of the following is not an arithmetic operator in Java?

● A) +

● B) -

● C) &

● D) %
Answer: C

19. What will be the output of 5 + 2 * 3 in Java?-

● A) 21

● B) 11

● C) 13

● D) 17
Answer: B

Java Input and Output:

20. Which class is used for input in Java?

● 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?

● A) Scanner input = new Scanner(System.in); String name = input.nextLine();

● B) BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String


name = br.readLine();

● C) Both A and B

● D) None of the above


Answer: C

Java Expressions & Blocks:

22. What is an expression in Java?-

● A) A sequence of variables and operators that computes a value

● B) A block of code

● C) A single statement

● D) None of the above


Answer: A

23. Which of the following defines a block in Java?-

● A) A single line of code

● B) A sequence of statements enclosed in curly braces

● C) A condition statement

● D) None of the above


Answer: B

Java Comments:

24. Which of the following is a valid single-line comment in Java?-

● A) /* Comment */

● B) # Comment

● C) // Comment

● D) <-- Comment -->


Answer: C

25. Which of the following is used for multi-line comments in Java?-

● A) // Comment

● B) /* Comment */

● C) ### Comment
● D) !! Comment
Answer: B

Java Flow Control:

26. Which statement is used for decision-making in Java?-

● A) if-else

● B) switch

● C) for

● D) Both A and B
Answer: D

Java if...else:

27. What is the output of the following code?-

java

Copy code

if (5 > 2) {

System.out.println("True");

} else {

System.out.println("False");

● A) True

● B) False

● C) Error

● D) None of the above


Answer: A

Java switch Statement:

28. What is the purpose of the switch statement in Java?-

● A) To execute a block of code based on the value of a variable

● B) To loop over a set of values

● C) To terminate a loop

● D) None of the above


Answer: A

29. What is the default case in a switch statement?-

● A) It is mandatory
● B) It is executed if no matching case is found

● C) It comes before all other cases

● D) It is executed when all cases are executed


Answer: B

Java for Loop:

30. What is the correct syntax of a for loop in Java?-

● A) for (initialization; condition; update) { }

● B) for (initialization, condition, update) { }

● C) for initialization; condition; update { }

● D) for (condition, initialization, update) { }


Answer: A

31. Which of the following is true about for loops in Java?-

● A) A for loop can have multiple initializations

● B) A for loop can have multiple conditions

● C) Both A and B

● D) None of the above


Answer: C

Java for-each Loop:

32. What is the purpose of the for-each loop in Java?-

● A) To iterate over arrays or collections

● B) To execute a block of code a fixed number of times

● C) To iterate over a variable number of values

● D) None of the above


Answer: A

33. Which of the following is a valid for-each loop syntax?

● A) for (type variable : array) { }

● B) foreach (type variable : array) { }

● C) for (type : array : variable) { }

● D) for (array variable type) { }


Answer: A

Java while Loop:

34. What is the correct syntax for a while loop in Java?-

● A) while (condition) { }
● B) while condition { }

● C) while: condition { }

● D) while(condition);
Answer: A

35. Which of the following is true about the while loop?-

● A) It checks the condition before executing the loop body

● B) It checks the condition after executing the loop body

● C) It executes at least once

● D) None of the above


Answer: A

Java break Statement:

36. What is the purpose of the break statement in Java?-

● A) To exit a loop or switch statement

● B) To pause the execution

● C) To continue the next iteration of the loop

● D) None of the above


Answer: A

Java continue Statement:

37. What is the function of the continue statement?-

● A) It skips the current iteration and continues with the next one

● B) It stops the loop

● C) It terminates the loop

● D) It throws an exception
Answer: A

Additional Questions:

39. Which of the following is not a reserved keyword in Java?

● A) class

● B) try

● C) new

● D) include
Answer: D

40. Which of these statements is true about Java?-


● A) Java is platform-dependent

● B) Java does not support multiple inheritance

● C) Java supports operator overloading

● D) Java does not support object-oriented programming


Answer: B

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

● D) none of the above


Answer: B

43. Which of the following is not a data type in Java?

● A) float

● B) double

● C) string

● D) boolean
Answer: C

44. What does the "++" operator do in Java?-

● A) Decreases the value by one

● B) Increases the value by one

● C) Multiplies the value by two

● D) Divides the value by two


Answer: B

45. Which loop guarantees to execute at least once?-

● A) for loop

● B) while loop

● C) do-while loop
● D) None of the above
Answer: C

46. Which of the following is not a valid loop in Java?-

● A) for

● B) while

● C) loop

● D) do-while
Answer: C

47. Which symbol is used to compare two values in Java?-

● A) =

● B) ==

● C) !=

● D) <>
Answer: B

48. Which of the following statements is true about Java classes?

● A) A class must contain at least one method

● B) A class cannot extend more than one class

● C) A class cannot have any data members

● D) A class must be abstract


Answer: B

49. Which of the following is true about constructors in Java?-

● A) A constructor must have the same name as the class

● B) A constructor must always return a value

● C) A class can have only one constructor

● D) A constructor can be abstract


Answer: A
● Which of the following features is not supported by C but is supported by C++?
a) Function overloading
b) Inline functions
c) Pointers
d) None of the above

● Which operator has the highest precedence in Java?


a) Multiplication (*)
b) Addition (+)
c) Increment (++)
d) Logical AND (&&)
50. Which of the following control structures is used to exit from a loop?-

● A) break

● B) continue

● C) return

● D) exit
Answer: A

Java JVM, JRE, and JDK

1. What does JVM stand for?

o a) Java Virtual Method

o b) Java Verified Machine

o c) Java Virtual Machine

o d) None of the above


Answer: c

2. Which of the following is responsible for running Java programs?

o a) JDK

o b) JVM

o c) JRE

o d) Bytecode
Answer: b

Which component is used for Java development?

o a) JRE

o b) JVM

o c) JDK

o d) None of the above


Answer: c

3. What is the primary role of the JIT compiler in JVM?

o a) Debugging Java programs

o b) Converting bytecode to machine code

o c) Compiling Java code into bytecode

o d) None of the above


Answer: b
Differences Between C and C++

6. Which feature of C++ is not available in C?

o a) Object-oriented programming

o b) Structured programming

o c) Pointers

o d) None of the above


Answer: a

7. Which language allows operator overloading?

o a) C

o b) C++

o c) Both

o d) None
Answer: b

8. What is the key difference between C and C++?

o a) C++ supports object-oriented programming

o b) C++ is faster than C

o c) C supports exception handling

o d) None of the above


Answer: a

Java Variables and Data Types

9. Which data type is used to store decimal numbers in Java?

o a) int

o b) float

o c) double

o d) Both b and c
Answer: d

10. What is the size of a char in Java?

o a) 1 byte

o b) 2 bytes

o c) 4 bytes
o d) Platform-dependent
Answer: b

11. What keyword is used to declare constants in Java?

o a) final

o b) const

o c) static

o d) None
Answer: a

Java Operators

12. Which operator is used for modulus operation?

o a) %

o b) //

o c) &&

o d) None
Answer: a

13. What is the output of 5 + 2 * 3 in Java?

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

Java Input and Output

15. Which method is used to read a line of text in Java?

o a) nextLine()
o b) readLine()

o c) readline()

o d) None
Answer: a

16. What does System.out.println() do?

o a) Prints the text in the same line

o b) Prints the text with a newline

o c) Throws an exception

o d) None of the above


Answer: b

Java JVM, JRE, and JDK

1. Which of the following is not a part of the JDK?

o a) Compiler

o b) JVM

o c) API libraries

o d) Operating System
Answer: d

2. Which command is used to compile a Java program?

o a) javac

o b) java

o c) compile

o d) None of the above


Answer: a

3. What does javap do in Java?

o a) Compiles Java source code

o b) Disassembles class files

o c) Executes Java programs

o d) None of the above


Answer: b

Differences Between C and C++

4. Which language has better support for dynamic memory allocation?


o a) C

o b) C++

o c) Both are the same

o d) None
Answer: b

Java Variables and Data Types

6. Which of the following is not a primitive data type in Java?

o a) int

o b) boolean

o c) String

o d) float
Answer: c

7. What is the default value of a local variable in Java?

o a) 0

o b) null

o c) Depends on the data type

o d) No default value
Answer: d

8. What is the maximum value of a byte in Java?

o a) 128

o b) 255

o c) 127

o d) None of the above


Answer: c

Java Operators

9. What is the result of the expression 7 % -3 in Java?

o a) 1

o b) -1

o c) 0
o d) None of the above
Answer: a

10. Which operator is used to concatenate two strings in Java?

o a) &

o b) +

o c) *

o d) None of the above


Answer: b

Java Input and Output

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

13. Which stream is used to output data to the console?

o a) InputStream

o b) OutputStream

o c) System.in

o d) System.out
Answer: d

14. What is the correct way to close a Scanner object in Java?

o a) scanner.close();

o b) scanner.end();

o c) scanner.quit();

o d) None of the above


Answer: a

15. What is the output of the following code?

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

What will be the output of the following code?

java

Copy code

int x = 10, y = 20;

System.out.println(x + ++y + y++);

● a) 51

● b) 52

● c) 50

● d) None of the above


Answer: b

1. Java Data Types

1. What is the size of a char in Java?

o a) 1 byte

o b) 2 bytes

o c) 4 bytes

o d) 8 bytes
Answer: b

2. Which of the following is a valid declaration of a float in Java?

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

3. What happens when you assign a long value to a float variable?

o a) Compile-time error

o b) Data loss may occur


o c) Implicit casting happens

o d) None of the above


Answer: b

2. Java Operators

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

java

Copy code

int x = 10, y = 20;

System.out.println(x + ++y + y++);

o a) 51

o b) 52

o c) 50

o d) None of the above


Answer: b

5. Which operator is used for bitwise AND?

o a) &

o b) &&

o c) |

o d) ~
Answer: a

3. Java Input and Output

6. What will System.out.println("hello" + null); print?

o a) Compile-time error

o b) "hellonull"

o c) Runtime Exception

o d) None of the above


Answer: b

7. Which statement is true for BufferedReader over Scanner?

o a) BufferedReader is faster.

o b) Scanner is faster.
o c) Both are equally fast.

o d) None of the above.


Answer: a

4. Java Expressions & Blocks

8. What is the output of the following block of code?

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

9. Which of these is true about Java blocks?

o a) Blocks can have their own local variables.

o b) Blocks cannot declare new variables.

o c) Blocks can only be used inside loops.

o d) None of the above


Answer: a

5. Java if-else

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

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 d) None of the above


Answer: a

11. Can else be used without if in Java?

o a) Yes

o b) No

o c) Only in ternary operators

o d) None of the above


Answer: b

6. Java Switch Statement

12. What happens if no case matches in a switch statement?

o a) The program crashes.

o b) Default block executes if present.

o c) Nothing happens.

o d) None of the above


Answer: b

7. Java for Loop

14. Which of these is not a valid for loop declaration?

o a) for (int i = 0; i < 10; i++)

o b) for ( ; i < 10; i++)

o c) for (int i = 0; ; i++)

o d) for (int i = 0, j = 0; i < 10 && j < 5; i++, j++)


Answer: b

8. Java for-each Loop

16. What is required to use a for-each loop?

o a) A collection or array
o b) A defined index

o c) A continue statement

o d) None of the above


Answer: a
18 What does JRE consist of?

a) JVM + Libraries

b) Libraries + Development Tools

c) JVM + Development Tools

d) Only JVM

UNIT : 2

Java OOP Basics

1. Which of the following is not a feature of OOP in Java? a) Inheritance


b) Encapsulation
c) Polymorphism
d) Compilation

2. Which keyword is used to create an object in Java?-

a) new
b) class
c) object
d) this

3. What is the return type of a constructor? a) void


b) int
c) No return type
d) Object

4. What is the default value of a string in a class if not initialized? a) null


b) ""
c) 0
d) "undefined"

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.

6. Which of these is a valid return type of a method in Java? –

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

8. The keyword final in Java is used for? a) Preventing inheritance of a class


b) Preventing overriding of a method
c) Declaring a constant variable
d) All of the above

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

Java Classes, Objects, and Methods

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();

14. Which method is used to compare two strings in Java? a) equals()


b) ==
c) compare()
d) equalsIgnoreCase()

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()

Java Inheritance & Polymorphism

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

21. Which of the following is an example of method overriding? a) A subclass providing a


different implementation of a method from its superclass
b) Two methods in the same class having the same name but different parameters
c) A method having a different return type
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

26. Which of the following is NOT a type of polymorphism in Java?- a) Compile-time


b) Run-time
c) Overloading
d) Method-hiding

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

28. Can an abstract class have a constructor in Java? a) Yes


b) No
c) Only if it is public
d) Only if it has abstract methods

29. Which is the correct syntax to implement an interface? a) class A implements B {}


b) class A extends B {}
c) class A imports B {}
d) class A uses B {}

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.

Java Other Types of Classes

31. What is a nested class in Java?- a) A class inside another class


b) A class inside a package
c) A class extending another class
d) None of the above

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

34. Which keyword is used to create a singleton class in Java? a) final


b) static
c) private constructor
d) abstract

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

40. Which method is used to compare two enums? a) compareTo()


b) ==
c) equals()
d) compare()

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.

Java OOP Basics

1. d) Compilation

2. a) new

3. c) No return type

4. a) null

5. a) It refers to the current object.

6. b) void

7. a) private

8. d) All of the above

9. b) A method calling itself

10. b) To check if an object belongs to a specific class or subclass

Java Classes, Objects, and Methods

11. b) A obj = new A();


12. b) Default constructor

13. a) String s = "Hello";

14. a) equals()

15. c) The class cannot be subclassed.

16. a) It is treated as a constructor.

17. b) Static methods cannot access instance variables.

18. b) To refer to the current object

19. b) length()

Java Inheritance & Polymorphism

20. c) Both a and b

21. a) A subclass providing a different implementation of a method from its superclass

22. b) super

23. a) The ability of an object to take many forms

24. c) They must be declared in abstract classes.

25. b) Using interfaces

26. d) Method-hiding

27. d) All of the above

28. a) Yes

29. a) class A implements B {}

30. b) Methods must have different parameter lists.

Java Other Types of Classes

31. a) A class inside another class

32. a) A nested class that is declared as static

33. a) To define a class and instantiate it in a single step

34. c) private constructor

35. a) A special type used to define collections of constants

36. b) Enum can have fields, constructors, and methods.

37. c) Using dot notation

38. c) It must implement an interface.

39. a) Array

40. a) compareTo()
41. a) Inner class can access outer class members, while static nested class cannot.

Java OOP (Other Types of Classes)

1. What is a nested class in Java?-


a) A class inside a package
b) A class that inherits from another class
c) A class inside another class
d) A class inside a static block

2. Which of the following is true about a static nested class?


a) It can access non-static members of the outer class.
b) It can only access static members of the outer class.
c) It can be instantiated without an instance of the outer class.
d) Both b and c

3. What is the purpose of an anonymous class in Java?-


a) To declare a class inside another class
b) To define a class and instantiate it in a single step
c) To create a static nested class
d) To declare an abstract class

4. Which of the following is true about singleton classes in Java?-


a) Singleton classes can have multiple instances.
b) A singleton class must have a public constructor.
c) Only one instance of a singleton class can exist.
d) A singleton class can only be created using interfaces.

5. How do you create a singleton class in Java?


a) By declaring all the class variables as static
b) By creating a private constructor and a static method for object creation
c) By implementing multiple constructors
d) By using the final keyword in class declaration

6. What is an enum in Java?-


a) A special class used to represent a fixed set of constants
b) A class that can hold only strings
c) A subclass of Object
d) A class that can only be used in loops

7. Which of the following is true about an enum constructor in Java?


a) Enum constructors can only be private.
b) Enum constructors must always be public.
c) Enum constructors cannot have parameters.
d) Enum cannot have constructors.

8. Which method is used to retrieve all values of an enum in Java?-


a) getValues()
b) values()
c) getEnum()
d) enumValues()

9. How do you access a specific enum constant in Java?-


a) Using the 'new' keyword
b) Using dot notation (e.g., EnumName.CONSTANT)
c) Using the 'get' method
d) Using the 'find' method

10. Which method is used to compare two enum constants in Java?-


a) equals()
b) compareTo()
c) compare()
d) ==

 What is a nested class in Java?


c) A class inside another class

 Which of the following is true about a static nested class?


d) Both b and c
(It can only access static members of the outer class and can be instantiated without an instance of
the outer class)

 What is the purpose of an anonymous class in Java?


b) To define a class and instantiate it in a single step

 Which of the following is true about singleton classes in Java?


c) Only one instance of a singleton class can exist

 How do you create a singleton class in Java?


b) By creating a private constructor and a static method for object creation

 What is an enum in Java?


a) A special class used to represent a fixed set of constants

 Which of the following is true about an enum constructor in Java?


a) Enum constructors can only be private

 Which method is used to retrieve all values of an enum in Java?


b) values()

 How do you access a specific enum constant in Java?


b) Using dot notation (e.g., EnumName.CONSTANT)

 Which method is used to compare two enum constants in Java?


b) compareTo()

Java Classes and Objects, Methods, and Constructors

1. What will happen if a class has no constructor defined?


o a) Compilation error

o b) Java provides a default no-argument constructor

o c) The program will crash

o d) None of the above


Answer: b

Java Strings

4. What is the output of this code?

java

Copy code

String s1 = "abc";

String s2 = new String("abc");

System.out.println(s1 == s2);

o a) true

o b) false

o c) Compilation error

o d) Runtime exception
Answer: b

5. Which method would you use to compare two strings lexicographically?

o a) compareTo()

o b) equals()

o c) ==

o d) None of the above


Answer: a

Java Access Modifiers

Java this and final Keywords

7. Can this be used inside a static method?

o a) Yes

o b) No

o c) It depends on the compiler


o d) None of the above
Answer: b

8. Which of the following is true about a final variable?

o a) It must be initialized at the time of declaration.

o b) Its value cannot change once initialized.

o c) Both a and b.

o d) None of the above.


Answer: c

Java Inheritance and Polymorphism

9. What happens if a subclass does not override an inherited abstract method?

o a) Compilation error

o b) It becomes an abstract class

o c) The program runs normally

o d) Runtime error
Answer: b

10. What is method overriding?

● a) Methods in a subclass have the same name as in the superclass with the same signature.

● b) Changing the return type only.

● c) Changing access modifiers.

● d) None of the above.


Answer: a

12. What is the correct way to define a static nested class?

java

Copy code

class Outer {

static class Nested { }

● a) Outer.Nested nestedObj = new Outer().new Nested();

● b) Outer.Nested nestedObj = new Outer.Nested();

● 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

● c) Allowed but will throw an error when accessed

● d) None of the above


Answer: a

Java Recursion

1. Which of the following statements about recursion is true?

o a) Every recursive method must have a base case.

o b) Recursive methods consume less memory than iterative methods.

o c) Recursion is always faster than iteration.

o d) None of the above.


Answer: a

2. What is the output of this code?

java

Copy code

public class Test {

public static int factorial(int n) {

if (n == 0) {

return 1;

return n * factorial(n - 1);

public static void main(String[] args) {

System.out.println(factorial(5));

o a) 120
o b) 24

o c) Compilation error

o d) Runtime exception
Answer: a

3. Which of the following problems is commonly solved using recursion?

o a) Fibonacci sequence

o b) Sorting algorithms

o c) Tree traversals

o d) All of the above


Answer: d

4. What will happen if a recursive method has multiple base cases?

o a) Only the first base case will be checked.

o b) All base cases will be checked until one matches.

o c) It causes a compilation error.

o d) It causes a runtime exception.


Answer: b

Java Interfaces

6. What is true about default methods in interfaces?

o a) They must be implemented in the implementing class.

o b) They provide a concrete implementation within the interface.

o c) They cannot be overridden by the implementing class.

o d) None of the above.


Answer: b

7. What is the output of the following code?

java

Copy code

interface A {

void display();

public class Test implements A {

public void display() {


System.out.println("Interface Method");

public static void main(String[] args) {

A obj = new Test();

obj.display();

o a) Interface Method

o b) Compilation error

o c) Runtime exception

o d) None of the above


Answer: a

8. What happens if a class implements two interfaces with the same default method?

o a) Compilation error

o b) The method must be overridden in the implementing class.

o c) The default implementation from the first interface is used.

o d) None of the above


Answer: b

9. Which is NOT true about interfaces in Java?

o a) An interface can have static methods.

o b) An interface can declare private methods.

o c) An interface can implement another interface.

o d) An interface cannot have constructors.


Answer: c

Java OOP Basics

1. What is true about constructors in Java?

o a) Constructors can be inherited.

o b) Constructors can be overridden.

o c) Constructors cannot be static.

o d) None of the above.


Answer: c
Java Strings

3. What does the following code output?

java

Copy code

String str1 = "Java";

String str2 = "Java";

String str3 = new String("Java");

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

4. Which of the following creates an immutable object in Java?

o a) StringBuffer

o b) StringBuilder

o c) String

o d) None of the above


Answer: c

Inheritance and Polymorphism

5. Which is true about method overloading in Java?

o a) The methods must have the same return type.

o b) The methods must have different parameter lists.

o c) Method overloading happens at runtime.

o d) None of the above.


Answer: b

Nested and Anonymous Classes

7. Can a static nested class access non-static members of the enclosing class?

o a) Yes
o b) No

o c) Only if they are declared as final.

o d) None of the above.


Answer: b

8. What is true about an anonymous class in Java?

o a) It must implement an interface or extend a class.

o b) It can have constructors.

o c) It can be declared static.

o d) All of the above.


Answer: a

Enum and Singleton

9. Which is true about enums in Java?

o a) Enums can have methods.

o b) Enums cannot be instantiated.

o c) Enums can implement interfaces.

o d) All of the above.


Answer: d

11. Can we call a constructor explicitly inside another constructor in the same class?

a) No

b) Yes, using this()

c) Yes, using the class name

d) None of the above

10. What is the output of this code?

String s1 = "abc";

String s2 = new String("abc");

System.out.println(s1 == s2);

a) true

b) false

c) Compilation error
d) Runtime exception

What is true about object references in Java?

class Test {

int x = 5;

public class Main {

public static void main(String[] args) {

Test obj1 = new Test();

Test obj2 = obj1;

obj1.x = 10;

System.out.println(obj2.x);

a) 5

b) 10

c) Compile-time error

d) Runtime exception

What is the purpose of the super keyword in Java?

a) To invoke a superclass constructor.

b) To invoke a superclass method.

c) Both a and b.

d) None of the above.


UNIT : 3

Java Exception Handling

1. What is an exception in Java?


a) A compilation error
b) A runtime event that disrupts the normal flow of the program
c) A syntax error
d) A logic error

2. Which is true about packages?

a) Java automatically imports all classes in java.lang.

b) Custom packages must be explicitly imported.

c) Both a and b.

d) None of the above.

3. What will happen if a class in a package does not have a public modifier?

a) It can be accessed from any package.

b) It is accessible only within the same package.

c) Compilation error.

d) None of the above.

4. Which of the following is not a type of exception in Java?


a) Checked exception
b) Unchecked exception
c) Runtime exception
d) Logic exception
5. Which class is the superclass of all exceptions in Java?
a) Error
b) Exception
c) RuntimeException
d) Throwable
6. What is the purpose of the try block in exception handling?-
a) To throw an exception
b) To test a block of code for errors
c) To finally release resources
d) To catch the exception
7. Which block is mandatory when using try in Java?
a) finally
b) throw
c) catch
d) None of the above
8. What does the catch block do in exception handling?-
a) Defines the code to execute after an exception occurs
b) Specifies how to handle the exception thrown in the try block
c) Declares the exception type
d) Re-throws the exception
9. Which of the following keywords is used to throw an exception manually?
a) throws
b) throw
c) try
d) catch
10. What is the purpose of the throws keyword?
a) To throw multiple exceptions
b) To declare that a method can throw an exception
c) To catch an exception
d) To suppress exceptions
11. Which of these exceptions are checked exceptions?
a) NullPointerException
b) FileNotFoundException
c) ArithmeticException
d) ArrayIndexOutOfBoundsException
12. Which of the following is an unchecked exception in Java?-
a) IOException
b) ClassNotFoundException
c) ArithmeticException
d) SQLException

Java try...catch, throw, throws

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

Java Annotations and Types

18. Which of the following is not a built-in annotation in Java?


a) @Override
b) @SuppressWarnings
c) @Entity
d) @Deprecated

19. Which annotation indicates that a method overrides a method in a superclass?-


a) @Deprecated
b) @Override
c) @Inherited
d) @SuppressWarnings
20. What is the purpose of the @SuppressWarnings annotation in Java?
a) To suppress compile-time warnings
b) To suppress runtime exceptions
c) To suppress all exceptions
d) To suppress variable declarations

Java Exception Handling

1. What is an exception in Java?


b) A runtime event that disrupts the normal flow of the program
2. Which of the following is not a type of exception in Java?
d) Logic exception
3. Which class is the superclass of all exceptions in Java?
d) Throwable
4. What is the purpose of the try block in exception handling?
b) To test a block of code for errors
5. Which block is mandatory when using try in Java?
d) None of the above
(You can have a try block without catch by using finally.)
6. What does the catch block do in exception handling?
b) Specifies how to handle the exception thrown in the try block
7. Which of the following keywords is used to throw an exception manually?
b) throw
8. What is the purpose of the throws keyword?
b) To declare that a method can throw an exception
9. Which of these exceptions are checked exceptions?
b) FileNotFoundException
10. Which of the following is an unchecked exception in Java?
c) ArithmeticException

Java try...catch, throw, throws

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 Annotations and Types

18. Which of the following is not a built-in annotation in Java?


c) @Entity
(@Entity is part of Java Persistence API, not core Java.)
19. Which annotation indicates that a method overrides a method in a superclass?
b) @Override
20. What is the purpose of the @SuppressWarnings annotation in Java?
a) To suppress compile-time warnings

Java Exception Handling

1. What happens if an exception is not caught in a Java program?


o a) Compilation error
o b) The program continues execution
o c) The program terminates abnormally
o d) None of the above
Answer: c
2. Which of the following is a checked exception?
o a) ArithmeticException
o b) NullPointerException
o c) IOException
o d) ArrayIndexOutOfBoundsException
Answer: c
3. What is the purpose of the throws keyword?
o a) To declare exceptions that a method might throw
o b) To handle exceptions explicitly
o c) Both a and b
o d) None of the above
Answer: a
4. Can multiple catch blocks be executed for a single try block?
o a) Yes
o b) No
o c) Only for runtime exceptions
o d) None of the above
Answer: b
5. What is the correct order of exceptions in catch blocks?
o a) General to specific
o b) Specific to general
o c) Random order
o d) None of the above
Answer: b
6. What is the output of this code?

java

Copy code

try {

throw new NullPointerException();

} catch (Exception e) {

System.out.println("Caught Exception");

oa) Compilation error


ob) Caught Exception
oc) Runtime exception
od) None of the above
Answer: b
7. Can we write a try block without a catch block?
o a) Yes, if followed by finally
o b) No
o c) Only in Java 8 and later
o d) None of the above
Answer: a
8. Which block always executes in exception handling?
o a) try
o b) catch
o c) finally
o d) All of the above
Answer: c

Java Annotations

11. What is the purpose of the @Override annotation?


o a) To override a method in the parent class
o b) To mark a method as deprecated
o c) To ensure the method signature matches an overridden method
o d) None of the above
Answer: c
12. What will happen if an incorrect method name is annotated with @Override?
o a) Compilation error
o b) Runtime error
o c) The annotation is ignored
o d) None of the above
Answer: a

Java Multithreading

14. Which method stops a thread in Java?


o a) stop()
o b) interrupt()
o c) Both a and b
o d) None of the above
Answer: b
15. What will happen if we call start() on a thread that has already been started?
o a) The thread restarts.
o b) The thread runs normally.
o c) Throws IllegalThreadStateException.
o d) None of the above.
Answer: c
16. Which is true about the wait() method?
o a) It releases the lock on the object.
o b) It can only be called inside synchronized blocks.
o c) Both a and b
o d) None of the above
Answer: c
User-Defined Packages

21. What is the purpose of the package keyword in Java?


o a) To group classes together
o b) To make classes accessible globally
o c) To avoid naming conflicts
o d) Both a and c
Answer: d
22. What is the correct syntax for declaring a package?

java

Copy code

// Fill in the blank

______ 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

26. Which of these is a marker annotation?

a) @Override

b) @Deprecated

c) @FunctionalInterface

d) All of the above

27 .Which is NOT a valid state in the thread lifecycle?

a) New

b) Blocked

c) Running

d) Terminated

UNIT 4 :
String Classes and Operations

1. What is the output of the following code?

java

Copy code

String str = "Java";

str.concat(" Programming");

System.out.println(str);

o a) Java Programming

o b) Java

o c) Compilation error

o d) Runtime exception
Answer: b

2. Which method is used to compare two strings ignoring case?

o a) compareTo()

o b) equals()

o c) equalsIgnoreCase()

o d) toLowerCase()
Answer: c

3. What happens when a String is modified?

o a) The existing object is modified.

o b) A new object is created.

o c) Both a and b depending on the method used.

o d) None of the above.


Answer: b

Arrays

5. What will be the output of this code?

java

Copy code

int[] arr = {1, 2, 3, 4, 5};

System.out.println(arr[5]);
o a) 5

o b) Compilation error

o c) Runtime exception

o d) None of the above


Answer: c

6. Which of the following is NOT a valid operation on a 2D array?

o a) Accessing elements using array[i][j].

o b) Storing arrays of different data types.

o c) Initializing with uneven row lengths.

o d) Declaring without specifying dimensions.


Answer: b

Collections Framework

8. Which method is used to remove all elements from a List?

o a) deleteAll()

o b) clear()

o c) removeAll()

o d) None of the above


Answer: b

9. Which of the following operations are faster in a LinkedList than in an ArrayList?

o a) Random access

o b) Insertion and deletion in the middle

o c) Iterating over the list

o d) None of the above


Answer: b

I/O Streams

14. What is the difference between Reader and InputStream?

o a) Reader is for character streams, InputStream is for byte streams.

o b) InputStream is faster than Reader.

o c) Reader can handle binary data.


o d) None of the above.
Answer: a

15. What happens if close() is called on an already closed stream?

o a) It throws an exception.

o b) It silently ignores the call.

o c) It corrupts the stream.

o d) None of the above.


Answer: b

16. What will the following code output?

java

Copy code

File file = new File("test.txt");

System.out.println(file.isDirectory());

o a) true

o b) false

o c) Compilation error

o d) Runtime exception
Answer: b

17. Which of these is a character-oriented output stream?

o a) FileOutputStream

o b) PrintWriter

o c) DataOutputStream

o d) BufferedOutputStream
Answer: b

21. Which interface is the root of the Java Collections Framework?

a) List

b) Collection

c) Iterable

d) Map

22. What is the default capacity of an ArrayList?

a) 10
b) 16

Which of these classes is a byte-oriented stream?

a) FileReader

b) FileInputStream

c) BufferedReader

d) PrintWriter

Which of the following statements about BufferedReader is true?

a) It reads data line by line.

b) It is faster than FileReader.

c) It reduces the number of I/O operations.

d) All of the above.

Which class is used to write primitive data types to a file?

a) ObjectOutputStream

b) DataOutputStream

c) PrintWriter

d) FileWriter

What is the result of the following code?

String str = "abcdef";

System.out.println(str.substring(1, 4));

a) abc

b) bcd

c) abcd

d) Runtime exception

Which of the following statements about the Set interface is true?

a) It allows duplicate elements.

b) It is a part of the java.util package and guarantees insertion order.

c) It extends the Collection interface and does not allow duplicate elements.

d) It is a synchronized collection by default.


UNIT 5 :

Database Applications with JDBC

1. What is the role of the DriverManager class in JDBC?

o a) Establishes a connection to the database.

o b) Manages database metadata.

o c) Provides an abstraction for driver-specific implementation.

o d) None of the above.


Answer: a

2. Which method is used to load a JDBC driver explicitly?

o a) Driver.load()

o b) Class.forName()

o c) DriverManager.registerDriver()

o d) None of the above.


Answer: b

3. What does ResultSet.next() do?

o a) Advances to the next row of the result set.

o b) Checks if there is a next row in the result set.

o c) Closes the result set.

o d) None of the above.


Answer: a

4. What type of driver is referred to as a "pure Java" driver?

o a) Type-1

o b) Type-2

o c) Type-3

o d) Type-4
Answer: d

5. Which method in JDBC is used to execute stored procedures?

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 c) Depends on the database

o d) None of the above


Answer: a

7. Which interface provides metadata about database tables?

o a) DatabaseMetaData

o b) ResultSetMetaData

o c) ConnectionMetaData

o d) None of the above


Answer: a

Java Persistence API (JPA)

11. Which of the following is NOT part of the JPA architecture?

o a) Persistence context

o b) Entity manager factory

o c) Hibernate context

o d) Persistence unit
Answer: c

12. Which method is used to persist an entity in JPA?

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 d) None of the above


Answer: a
14. Which annotation is used to define a named query in JPA?

o a) @NamedQuery

o b) @Query

o c) @PersistentQuery

o d) None of the above


Answer: a

15. Which method is used to retrieve a single entity using JPQL?

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)?

a) Simplify database schema creation

b) Provide an ORM framework for Java objects

c) Improve transaction performance

d) None of the above

Which annotation is used to specify a JPA entity?

17. a) @Table

b) @Entity

c) @PersistenceUnit

d) None of the above

What is the role of EntityManager in JPA?

a) Manage database schema creation

b) Handle CRUD operations and transactions

c) Provide metadata about entities

d)None of the above

18. What does JPQL stand for?

a) Java Programming Query Language

b) Java Persistent Query Language

c) Java Persistence Query Language

d)Java Performance Query Language

Which of the following is NOT a valid JPA relationship?

a) @OneToOne
b) @ManyToMany

c) @PrimaryToSecondary

d) @ManyToOne

What is the role of @Inheritance in JPA?

a) Map inheritance hierarchies in entities.

19. b) Specify primary keys.

c) Define foreign key constraints.

d) None of the above.

20. Which JPA provider is NOT commonly used?

a) Hibernate

b) EclipseLink

c) MyBatis

d) OpenJPA

21.

22. Which JPA provider is NOT commonly used?

o a) Hibernate

o b) EclipseLink

o c) MyBatis

o d) OpenJPA
Answer: c

23. What is the purpose of @Embedded in JPA?

o a) Define a relationship.

o b) Map a composite object to a single database column.

o c) Map a composite object to multiple database columns.

o d) None of the above.


Answer: c

24. Which file contains persistence unit definitions in JPA?

o a) application.properties

o b) orm.xml

o c) persistence.xml

o d) config.xml
Answer: c

25. What does the merge() method in JPA do?


o a) Deletes an entity.

o b) Updates a detached entity back into persistence context.

o c) Saves a new entity.

o d) None of the above.


Answer: b
c) 1

d) Depends on the JVM

You might also like