JAVA MCQ
JAVA MCQ
a) has-a relationship
b) Is-the relationship
c) Is-A relationship
d) Interface relationship
2. Which of the following options represents the main components of exception handling in java
a) Try ,catch,if,else,switch
b) Exception ,throw , catch,throws, try
c) Try,catch,final, throw,throws
d) Try,catch,throw,throws,finally
3. The wrapper class in java provides the mechanism to convert primitive into object and object
into _
a) Data
b) Primitive
c) Variable
d) Non-primitive
System.out.println(Sum(11,11));
System.out.println(Sum (11,11,11));
}
}
A) 11
11
B) Compile-Time Error
C) 22
33
D) 121
1331
5) Which among the following option can be used to make String mutable?
A) Declaring it as 'final'
B) Wrapping it in a StringBuffer
C) Applying the 'mutable' modifier
D) Using the 'const' keyword
6) class A
{
public A(String s)
{
System.out.print("A")
}
}
public class B extends A
{
public B(String s)
{
System.out.print("B");
}
public static void main(String[] args)
{
new B("C");
System.out.println(" ");
}
}
interface Addable{
int add(int a,int b);
}
public class LambdaExpressionExample6 {
public static void main(String[] args) {
catch (Exception e)
{
System.out.println ("error = " + e);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println ("ArrayIndexOutOfBoundsException");
}
}
}
Options:
A) Compiler error
B) Run time error
C) ArrayIndexOutOfBoundsException
D) Error Code is printed
E) Array is printed
Answer:
A) Compiler error Explanation:
ArrayIndexOutOfBoundsException has been already caught by base class Exception.
When a subclass exception is mentioned after base class exception, then error occurs.
Failed
passed
0
10
a) for(int i:arr){
sop(i);
}
b) for(int i =0;arr.length){
sop(i);
}
c) for(int i=0;arr.length;i++){
sop(i);
}
Output:
IT
Technical
a) Larger Number
b) Smaller Number
c) Larger Number:25
d) Smaller Number:18
16) which statement is correct if we want to connect the oracle database using the thin driver
provided by Oracle Corp.?
getConnection("jdbc::thin@localhost:1521:oracle","scott","tiger");
getConnection("jdbc:thin@localhost:1521:oracle","scott","tiger");
getConnection("jdbc::thin@localhost:1522:oracle","scott","tiger");
getConnection("jdbc::oracle@localhost:1521:thin","scott","tiger");
17) which of the following method is used to perform DML statemets in jdbc?
executeResult()
executeQuery()
executeUpdate()
execute()
32767
-32767
0
32769
A
B
0
ArithmeticException: Divide by Zero
import java.util.*;
class Bitset{
psvm{
BitSet b = new BitSet(5);
for(int j=0;j<5;++j){
b.set(j*2);
b.clear(4);
sop(b);
}
}
}
{0,2,6,8}
{0,2,4,6}
{0,2,4,8}
{4}
In Java, a char type can only hold a single character within single quotes.
int x = 5;
System.out.println(x++ + ++x);
- A. 10
- B. 11
- C. 12
- D. 13
- **Answer: D. 13**
- A. helloworld
- B. hello world
- C. hello + world
- D. Compilation Error
- **Answer: A. helloworld**
10. Which of the following statements is used to exit from a loop in Java?
- A. return
- B. exit
- C. break
- D. continue
- **Answer: C. break**
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is less than or equal to 5");
}
- A. x is greater than 5
- B. x is less than or equal to 5
- C. Compilation Error
- D. Runtime Error
- **Answer: A. x is greater than 5**
12. Which data structure is used to implement a Last-In-First-Out (LIFO) mechanism in Java?
- A. Queue
- B. Stack
- C. Array
- D. Linked List
- **Answer: B. Stack**
- A. 5
- B. 4
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
int x = 5;
System.out.println(x > 2 ? "Yes" : "No");
- A. Yes
- B. No
- C. true
- D. false
- **Answer: A. Yes**
17. What will be the value of 'x' after executing the following code snippet?
int x = 5;
x += 3;
- A. 5
- B. 3
- C. 8
- D. 15
- **Answer: C. 8**
- A. HELLO
- B. hello
- C. Compilation Error
- D. Runtime Error
- **Answer: A. HELLO**
int x = 10;
System.out.println(x / 0);
- A. Compilation Error
- B. Runtime Error
- C. 0
- D. Infinity
- **Answer: B. Runtime Error**
22. Which of the following is not a valid way to create an array in Java?
- A. int[] arr = new int[5];
- B. int[] arr = {1, 2, 3, 4, 5};
- C. int[] arr = new int[]{1, 2, 3, 4, 5};
- D. int[] arr = new int(5);
- **Answer: D. int[] arr = new int(5);**
- A. x is 10 End of if
- B. x is 10
- C. Compilation
Error
- D. Runtime Error
- **Answer: B. x is 10**
24. Which of the following statements is used to skip the current iteration in a loop in Java?
- A. return
- B. break
- C. skip
- D. continue
- **Answer: D. continue**
- A. 0
- B. null
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 0**
27. What will be the value of 'y' after executing the following code snippet?
int x = 5;
int y = x++;
- A. 5
- B. 6
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
28. Which of the following is a correct way to declare and initialize a two-dimensional array in
Java?
- A. int[][] arr = new int[3, 3];
- B. int[][] arr = new int[3][3];
- C. int[][] arr = {1, 2, 3, 4, 5};
- D. int[][] arr = new int{{1, 2, 3}, {4, 5, 6}};
- **Answer: B. int[][] arr = new int[3][3];**
int x = 5;
System.out.println(++x);
- A. 5
- B. 6
- C. Compilation Error
- D. Runtime Error
- **Answer: B. 6**
int x = 5;
int y = 3;
System.out.println(x > y ? x : y);
- A. 5
- B. 3
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
- A. h
- B. e
- C. l
- D. o
- **Answer: C. l**
int x = 5;
System.out.println(x--);
- A. 5
- B. 4
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
36. Which of the following is not a valid statement to create an object in Java?
- A. MyClass obj = new MyClass();
- B. new MyClass();
- C. obj = new MyClass();
- D. MyClass obj = MyClass();
- **Answer: D. MyClass obj = MyClass();**
int x = 5;
int y = 3;
System.out.println(x == y);
- A. true
- B. false
- C. Compilation Error
- D. Runtime Error
- **Answer: B. false**
- A. 5
- B. 6
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
Intermediate Level
11. What does the `static` keyword mean when applied to a method in Java?
- A. It indicates that the method cannot be overridden.
- B. It indicates that the method belongs to the class rather than the instance.
- C. It indicates that the method cannot be accessed outside the class.
- D. It indicates that the method is abstract.
- **Answer: B. It indicates that the method belongs to the class rather than the instance.**
12. Which of the following is not a valid statement about Java threads?
- A. Threads can be created by extending the `Thread` class.
- B. Threads can be created by implementing the `Runnable` interface.
- C. Java applications can have only one thread.
- D. The `sleep()` method is used to pause the execution of a thread.
- **Answer: C. Java applications can have only one thread.**
21. Which of the following statements about abstract classes in Java is true?
- A. Abstract classes cannot have constructors.
- B. Abstract classes can be instantiated.
- C. Abstract methods must be implemented in the abstract class itself.
- D. Abstract classes can be final.
- **Answer: A. Abstract classes cannot have constructors.**
25. Which of the following is true about the `Object` class in Java?
- A. All classes in Java are subclasses of the Object class.
- B. The Object class cannot be instantiated.
- C. The Object class does not have any methods.
- D. The Object class cannot be extended.
- **Answer: A. All classes in Java are subclasses of the Object class.**
int x = 5;
System.out.println(x > 2 && x < 10);
- A. true
- B. false
- C. Compilation Error
- D. Runtime Error
- **Answer: A. true**
27. Which of the following is a correct way to create an anonymous inner class in Java?
- A. `MyClass obj = new MyClass() {};`
- B. `MyClass obj = new anonymous MyClass() {};`
- C. `MyClass obj = new MyClass() new {};`
- D. `MyClass obj = new MyClass() => {};`
- **Answer: A. `MyClass obj = new MyClass() {};`**
- A. 1
- B. 4
- C. 5
- D. Compilation Error
- **Answer: C. 5**
29. Which of the following is not a valid type of inner class in Java?
- A. Static inner class
- B. Local inner class
- C. Abstract inner class
- D. Anonymous inner class
- **Answer: C. Abstract inner class**
- A. he
- B. ll
- C. llo
- D. Compilation Error
- **Answer: C. ll**
31. Which of the following is not a valid way to create a thread in Java?
- A. Extend the `Thread` class and override the `run()` method.
- B. Implement the `Runnable` interface and pass it to a `Thread` constructor.
- C. Use the `start()` method of the `Thread` class.
- D. Use the `run()` method of the `Thread` class.
- **Answer: D. Use the `run()` method of the `Thread` class.**
- A. 5
- B. 3
- C. 8
- D. 15
- **Answer: C. 8**
int x = 5;
System.out.println(x--);
- A. 5
- B. 4
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
annotations?
- A. Annotations can have constructors.
- B. Annotations can be used to modify the behavior of Java programs at runtime.
- C. Annotations are only applicable to methods.
- D. Annotations are used to define variables.
- **Answer: B. Annotations can be used to modify the behavior of Java programs at runtime.**
- A. Yes
- B. No
- C. true
- D. false
- **Answer: A. Yes**
- A. 0
- B. null
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 0**
- A. h
- B. e
- C. l
- D. o
- **Answer: C. l**
Advanced Level
int x = 10;
int y = x++ + ++x;
System.out.println(y);
- A. 20
- B. 21
- C. 22
- D. 23
- **Answer: D. 23**
- A. hello
- B. hello world
- C. world
- D. Compilation Error
- **Answer: A. hello**
- A. 0.0
- B. 1.0
- C. Infinity
- D. NaN (Not a Number)
- **Answer: D. NaN (Not a Number)**
- A. 12345
- B. 1 2 3 4 5
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 12345**
- A. [1, 2, 3, 4, 5]
- B. [2, 4, 6, 8, 10]
- C. Compilation Error
- D. Runtime Error
- **Answer: B. [2, 4, 6, 8, 10]**
int x = 5;
int y = 3;
System.out.println(x > y ? x : y);
- A. 5
- B. 3
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 5**
- A. hello
- B. hello world
- C. world
- D. Compilation Error
- **Answer: B. hello world**
20. What is the purpose of the `static` keyword when applied to a block of code in Java?
- A. It indicates that the block of code belongs to a subclass.
- B. It indicates that the block of code is executed only once when the class is loaded.
- C. It indicates that the block of code cannot be executed.
- D. It indicates that the block of code can be accessed only by static methods.
- **Answer: B. It indicates that the block of code is executed only once when the class is
loaded.**
- A. [1, 2, 3, 4, 5]
- B. [1, 3, 5]
- C. Compilation Error
- D. Runtime Error
- **Answer: B. [1, 3, 5]**
- A. he
- B. ll
- C. llo
- D. Compilation Error
- **Answer: B. ll**
int x = 5;
System.out.println(x += 3);
- A. 5
- B. 3
- C. 8
- D. 15
- **Answer: C. 8**
int x = 5;
System.out.println(++x * x--);
- A. 25
- B. 20
- C. 30
- D. 35
- **Answer: A. 25**
- A. 0
- B. null
- C. Compilation Error
- D. Runtime Error
- **Answer: A. 0**
int x = 5;
System.out.println(x > 2 && x < 10);
- A. true
- B. false
- C. Compilation Error
- D. Runtime Error
- **Answer: A. true**
- A. h
- B. e
- C. l
- D. o
- **Answer: C. l**
int x = 5;
System.out.println(x == 5 ? "Yes" : "No");
- A. Yes
- B. No
- C. true
- D. false
- **Answer: A. Yes**