25 High-Level MCQ Questions
Java Programming Construct
1. Which of the following best describes widening and narrowing conversions in
Java?
a) Both are implicit
b) Widening is implicit, narrowing is explicit
c) Both require casting
d) Widening requires casting
What is the output of the following code?
int a = 5;
[Link](++a + a++);
2. a) 11
b) 12
c) 10
d) 13
3. Which type of comment is used to generate documentation using javadoc?
a) //
b) /* */
c) /** */
d) <!-- -->
4. What happens if you declare a local variable with the same name as a class
member?
a) Compilation error
b) Runtime error
c) Local variable shadows the class member
d) Class member is used
5. Which of the following allows you to take multiple types of input in a single line
using Scanner?
a) next()
b) nextLine()
c) hasNext()
d) next<Type>() methods in sequence
Java Control Flow
6. What is the purpose of labeled break in Java?
a) Exit switch block
b) Exit a specific loop
c) Exit method
d) Restart loop
7. What is the key difference between while and do-while loops?
a) Syntax
b) do-while always executes once
c) while uses counter
d) do-while doesn't check condition
8. Which of the following statements is true about short-circuit evaluation?
a) Both sides are always evaluated
b) Only first operand is evaluated
c) Second operand is skipped if not needed
d) It's used only in ternary operators
9. Given a switch statement without a break, what is the behavior called when all
subsequent cases execute?
a) Jump
b) Skip
c) Fall-through
d) Skip-through
What will be the result of this code?
int x = 10;
if (x == 10)
if (x < 15)
[Link]("x < 15");
else
[Link]("x > 15");
10. a) x < 15
b) x > 15
c) Compilation error
d) No output
Java Arrays
11. Which of the following can dynamically resize during runtime?
a) Array
b) ArrayList
c) HashMap
d) LinkedList
12. What will int[] a = new int[5]; [Link](a[5]); output?
a) 0
b) null
c) ArrayIndexOutOfBoundsException
d) Compilation error
13. What is the default value of elements in a boolean[] array?
a) true
b) false
c) null
d) 0
14. How do you copy an array manually in Java?
a) [Link](a, b)
b) [Link]()
c) [Link]()
d) [Link]()
15. How many total elements in int[][] arr = new int[3][4];?
a) 7
b) 12
c) 3
d) 4
16. Which best describes a jagged array?
a) 2D array with different row sizes
b) Multi-dimensional equal-sized arrays
c) Arrays with null values
d) Circular arrays
17. What happens when we modify an array through a method?
a) It affects the original array
b) Only a copy is changed
c) Compilation error
d) Only the first element changes
18. Which loop is best suited for iterating over arrays where indexes are not needed?
a) while
b) for loop
c) for-each
d) do-while
Java Methods
19. What is method overloading?
a) Having two methods with different return types
b) Having two methods with same name but different parameters
c) Two methods with different access specifiers
d) None of the above
20. Which is true for a method that does not return anything?
a) Must return null
b) Cannot use return statement
c) Return type is void
d) Should be private
21. Which of the following is a valid static method usage?
a) Can access instance variables
b) Can call from another class without object
c) Can override in subclass
d) None of the above
22. Which library class has a method for absolute value?
a) [Link]()
b) [Link]()
c) [Link]()
d) [Link]()
23. What does the return keyword do inside a method?
a) Terminates method
b) Exits class
c) Skips loop
d) Repeats execution
24. What is the scope of a variable declared inside a method?
a) Whole class
b) Package
c) Only within the method
d) Inherited to subclasses
25. What is the main advantage of using methods in code?
a) Speed
b) Redundancy
c) Modularity and reusability
d) Compilation time
20 High-Level Coding Questions
Java Programming Construct & Control Flow
1. Write a program to simulate a basic calculator (add, subtract, multiply, divide)
using switch.
2. Create a program that takes a user's input name and age, then checks if the
person is eligible to vote (age >= 18).
3. Write a method that takes three integers and returns the greatest among them.
4. Take two integers and a character (‘+’, ‘-’, ‘*’, ‘/’) from the user. Perform the
operation based on the character.
5. Write a program to convert temperature from Celsius to Fahrenheit using a
method.
Java Arrays
6. Write a program that finds the maximum and minimum values in an array.
7. Sort an integer array in ascending order without using built-in sort functions.
8. Write a program to count the frequency of each element in an array.
9. Take an array from the user and print only the duplicate elements.
10. Write a method that merges two sorted arrays into one sorted array.
11. Write a program to rotate an array to the right by k steps.
12. Create a program to check whether two arrays are equal.
13. Write a program to reverse each row in a 2D array.
14. Given a matrix, write a method to transpose it.
Java Methods
15. Create a method to check if a given number is a palindrome.
16. Write a method that returns the factorial of a number.
17. Write a method that checks whether a given string is a palindrome.
18. Write a method that returns the sum of digits of a given number.
19. Write a method that accepts an array and returns the second largest number.
20. Write a program that implements method overloading for area calculation (circle,
rectangle, triangle).
25 Low-Level MCQ Questions
Java Programming Construct
1. Which of the following is a valid Java comment?
a) // This is a comment
b) /* This is a comment */
c) /** This is a doc comment */
d) All of the above
2. Which data type is used to store a true or false value?
a) int
b) boolean
c) char
d) String
3. What will be the output of [Link](5 + 3 * 2);?
a) 16
b) 11
c) 13
d) 10
4. Which keyword is used to define a variable in Java?
a) dim
b) let
c) var
d) No keyword is required; just the data type
5. Which operator is used for remainder in Java?
a) /
b) *
c) %
d) ^
6. Which of the following is not a primitive data type?
a) int
b) boolean
c) String
d) char
7. What is the result of typecasting int i = (int) 5.6;?
a) 5.6
b) 5
c) Compilation error
d) Runtime error
8. Which class is used for taking user input from the console?
a) SystemInput
b) Scanner
c) BufferReader
d) Console
Java Control Flow
9. What is the output of true && false?
a) true
b) false
c) error
d) null
10. Which control structure is used for multiple condition selection?
a) if
b) if-else
c) switch
d) while
11. Which loop executes at least once even if the condition is false?
a) for
b) while
c) do-while
d) None
12. Which keyword is used to exit a loop prematurely?
a) exit
b) stop
c) break
d) continue
13. What does the continue keyword do in a loop?
a) Exits the loop
b) Skips the current iteration
c) Repeats the last iteration
d) Skips the loop entirely
14. Which of these is a valid syntax of switch statement?
a) switch(i)
b) switch i:
c) if (switch i)
d) loop switch(i)
15. In if-else if ladder, how many conditions are checked?
a) Only the first one
b) All, until one is true
c) Only the last
d) Only the else block
Java Arrays
16. How do you declare an array in Java?
a) int array[];
b) array int[];
c) int[] array;
d) Both a and c
17. What is the index of the first element in an array?
a) 0
b) 1
c) -1
d) Depends on size
18. How can you find the number of elements in an array arr?
a) [Link]()
b) [Link]()
c) [Link]
d) [Link]()
19. Which loop is commonly used to access array elements?
a) do-while
b) if
c) for
d) switch
20. How do you change the value of an element in an array at index 2?
a) arr(2) = 10;
b) arr[2] = 10;
c) [Link](2, 10);
d) arr -> 2 = 10;
21. Which is the correct way to declare a 2D array in Java?
a) int[][] matrix = new int[3][3];
b) int matrix[][] = new int[3][3];
c) int matrix[3][3];
d) Both a and b
22. Which of the following best describes a 2D array?
a) A list of lists
b) A matrix
c) An array with rows and columns
d) All of the above
Java Methods
23. Which keyword is used to define a method?
a) method
b) function
c) void
d) return
24. Which of the following is true about static methods?
a) They require an object to be called
b) They belong to the class, not the instance
c) They can't return values
d) They must be private
25. What is the purpose of standard library methods like [Link]()?
a) To define new methods
b) To reuse built-in functionalities
c) To perform string operations only
d) None of the above
3 Coding Questions
1. Sum of Two Numbers (Using Scanner and Arithmetic Operators)
Problem:
Write a Java program to take two integers as input from the user and print their sum, difference,
and product.
2. Find Maximum Number in an Array
Problem:
Write a Java program that takes an array of integers and prints the largest number using a loop.
3. Define and Use a Method to Check Even/Odd
Problem:
Create a method isEven(int num) that returns true if the number is even, otherwise
false.
Take a number from the user and call the method to check.
4. Java Programming Constructs & Control Flow
1. Take two numbers from the user and print their sum.
2. Write a program to check if a number is even or odd.
3. Write a program to find the larger of two numbers using if-else.
4. Take a number as input and print the multiplication table of that number using a
loop.
5. Write a program that prints numbers from 1 to 10 using a while loop.
6. Write a program to find the sum of all numbers from 1 to n.
7. Take three marks as input and calculate the average of the marks.
8. Write a program to check whether a number is positive, negative, or zero.
9. Take a number from the user and check if it is divisible by 5 and 11.
25 Medium-Level MCQ Questions
Java Programming Construct
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);
2. What is the result of the expression 10 + 20 + "30"?
a) "3030"
b) 60
c) "102030"
d) "3030"
3. Which of these will not compile?
a) float f = 3.5f;
b) char ch = 'a';
c) boolean b = 0;
d) int i = 100;
4. What will [Link](5 / 2); output?
a) 2.5
b) 2.0
c) 2
d) 3
5. Which method from Scanner can read a float input?
a) nextFloat()
b) readFloat()
c) scanFloat()
d) getFloat()
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
7. Which of these data types takes the most memory?
a) int
b) float
c) double
d) char
8. What is the result of int x = (int) 'A';?
a) Compilation Error
b) ASCII value of 'A'
c) Runtime Error
d) 0
Java Control Flow
How many times will the following loop execute?
for(int i = 1; i < 5; i += 2) {
[Link](i);
}
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
11. What is the output?
int i = 0;
do {
i++;
} while (i < 0);
[Link](i);
a) 0
b) 1
c) Infinite loop
d) Error
12. Which condition is true for switch in Java?
a) Can use String values
b) Can use boolean values
c) Only int and char allowed
d) switch must have a default case
13. In a switch statement, if no break is used:
a) It skips that case
b) All cases after match execute ("fall-through")
c) It throws an error
d) It exits automatically
14. Logical AND (&&) and Bitwise AND (&) behave differently in:
a) Performance
b) Short-circuiting
c) Output
d) Data types
15. Which of the following will always execute at least once?
a) while(condition)
b) for(;;)
c) do { } while(condition);
d) if(condition)
Java Arrays
16. How do you initialize an array with values 1 to 5?
a) int[] a = {1,2,3,4,5};
b) int a[] = new int(1,2,3,4,5);
c) int a[] = new int[]{1,2,3,4,5};
d) Both a and c
17. What is the output of [Link] if int[] arr = new int[4];?
a) 3
b) 4
c) 5
d) Error
18. How do you print a 2D array's rows and columns?
a) Nested for loops
b) Single while loop
c) Only for-each
d) Cannot be printed directly
19. What value is stored in a newly created int[] of size 3?
a) null
b) 0
c) garbage value
d) depends on memory
20. What is the valid way to find the middle index of array arr?
a) arr[[Link] / 2]
b) [Link] / 2
c) arr[[Link]]
d) arr[[Link] - 1]
21. Which of these can store multiple rows and different column sizes?
a) 2D Matrix
b) Jagged Array
c) Multiset
d) Vector
22. Which is true about 2D arrays in Java?
a) All rows must be equal in length
b) You must initialize all elements
c) Rows can have different lengths
d) You can’t access them with nested loops
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()
24. What is true about calling a method inside another method?
a) It’s not allowed
b) It requires inheritance
c) It promotes reusability
d) It must be static
25. Which of the following is a valid use of static method?
a) Accessing instance variables
b) Calling from a different class using object
c) Calling without creating an object
d) None of the above
3 Medium-Level Coding Questions
1. Prime Number Check Using Method
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.
2. Search and Replace in Array
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.
3. Matrix Diagonal Sum
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.
4. Write a method to calculate the square of a number.
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).
7. Take a 2D array (matrix) and print it in row-wise format.
8. Write a method to reverse an array and return the reversed array.
9. Create a method that returns the factorial of a number.
10. Write a program using a method to check if a number is Armstrong or not.