0% found this document useful (0 votes)
2 views6 pages

_Java - MCQ Questions Coding Questions Medium Level

The document contains 25 medium-level multiple-choice questions (MCQs) covering Java programming concepts such as data types, control flow, arrays, and methods. Additionally, it includes 3 coding problems that require writing Java methods for tasks like prime number checking, search and replace in an array, and calculating the diagonal sum of a matrix. The document serves as a study resource for Java programming skills assessment.

Uploaded by

SHAIK ABDUL AHAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

_Java - MCQ Questions Coding Questions Medium Level

The document contains 25 medium-level multiple-choice questions (MCQs) covering Java programming concepts such as data types, control flow, arrays, and methods. Additionally, it includes 3 coding problems that require writing Java methods for tasks like prime number checking, search and replace in an array, and calculating the diagonal sum of a matrix. The document serves as a study resource for Java programming skills assessment.

Uploaded by

SHAIK ABDUL AHAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

✅ 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 System.out.println(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) {
System.out.print(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);
System.out.println(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 arr.length 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[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​

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.​

You might also like