_Java - MCQ Questions Coding Questions Medium Level
_Java - MCQ Questions Coding Questions Medium Level
1. Which of the following would be a valid way to convert a double to int in Java?
a) int i = doubleVar;
b) int i = (int) doubleVar;
c) int i = int(doubleVar);
d) int i = convert(int, doubleVar);
6. What happens if you try to access an index beyond array length?
a) Returns null
b) Returns 0
c) Throws ArrayIndexOutOfBoundsException
d) Wraps to first element
9. a) 2
b) 3
c) 4
d) Infinite
10.Which of the following will correctly exit both inner and outer loops in nested
loops?
a) Use two break statements
b) Use labeled break
c) Use return
d) Use continue twice
int i = 0;
do {
i++;
} while (i < 0);
System.out.println(i);
a) 0
b) 1
c) Infinite loop
d) Error
14.Logical AND (&&) and Bitwise AND (&) behave differently in:
a) Performance
b) Short-circuiting
c) Output
d) Data types
Java Arrays
20.What is the valid way to find the middle index of array arr?
a) arr[arr.length / 2]
b) arr.length / 2
c) arr[arr.length]
d) arr[arr.length - 1]
21.Which of these can store multiple rows and different column sizes?
a) 2D Matrix
b) Jagged Array
c) Multiset
d) Vector
Java Methods
23.Which is correct syntax to declare a method that returns an int and accepts two
parameters?
a) int myMethod(a, b)
b) int myMethod(int a, int b)
c) int myMethod(int, int)
d) int myMethod()
Problem:
Write a method isPrime(int n) that returns true if the number is prime.
Use Scanner to take a number and display whether it is prime or not.
Problem:
Write a Java program to take an array and a target value as input.
If the target exists, replace it with -1 and print the updated array.
Problem:
Take a 3x3 integer matrix from the user and write a method diagonalSum(int[][]
matrix) that returns the sum of the main diagonal.
4. Arrays & Methods
1. Write a program to take 5 numbers in an array and display the total sum.
2. Write a program to find the largest and smallest number in an array.
3. Take an array and print only even elements from it using a for loop.
5. Write a program that accepts a number and checks if it is a prime number.
6. Write a method that takes two numbers and returns the GCD (Greatest Common
Divisor).
8. Write a method to reverse an array and return the reversed array.