What is the type of error, if any, when two methods have the same method signature?
- Runtime error
- Logical error
- Syntax error
- No error
Answer
Syntax error
Reason — If two methods in the same class have the same method signature (i.e., the same method name and the same parameters), the Java compiler will report a syntax error. This is because Java does not allow duplicate method signatures in the same class as it cannot differentiate which method to call.
The advantage/s of user-defined methods are/is:
(i) Reusability
(ii) Complexity
(iii) Modularity
- Only (i)
- (ii) and (iii)
- Only (iii)
- (i) and (iii)
Answer
(i) and (iii)
Reason — User-defined methods promote reusability by allowing code to be written once and used multiple times, reducing redundancy. They also support modularity by breaking a program into smaller, manageable, and logically separate units, making it easier to understand, maintain, and debug.
Parameters which receive the values from the called method are termed as ............... parameters.
- actual
- formal
- reference
- class
Answer
formal
Reason — Parameters that receive values passed from the calling method are called formal parameters. They act as placeholders in the method definition to accept the actual values (actual parameters) during method invocation.
Which of the following is a valid method prototype?
- public int perform (int a;int b)
- public perform (int a, int b)
- public int perform (int a, int b)
- public perform int (int a, int b)
Answer
public int perform (int a, int b)
Reason — A valid method prototype in Java must specify the access modifier (public), the return type (int), the method name (perform), and the parameter list enclosed in parentheses with parameters separated by commas. The statement public int perform (int a, int b) correctly follows this syntax.
Which of the following is the CORRECT statement to invoke the method with the prototype int display(int a, char ch)?
- int m = display('A', 45);
- int m = display( );
- int m = display(A,45);
- int m = display(45,'A');
Answer
int m = display(45,'A');
Reason — The method display expects two parameters: the first is an int and the second is a char. Among the options, only display(45, 'A') passes the arguments in the correct order and with the correct types—45 as an integer and 'A' as a character literal.
Which of the following is the CORRECT java statement to convert the word RESPECT to lowercase?
- "RESPECT".tolowercase( );
- "RESPECT".toLowerCase( );
- toLowerCase("RESPECT");
- String.toLowerCase("RESPECT");
Answer
"RESPECT".toLowerCase( );
Reason — The method to convert a string to lowercase in Java is toLowerCase(), which is called on a string object. It is case-sensitive and must be written exactly as toLowerCase(). Therefore, "RESPECT".toLowerCase() correctly converts the string "RESPECT" to lowercase.
Which of the following are Wrapper classes?
(i) Boolean
(ii) boolean
(iii) character
(iv) Character
- (i) and (iv)
- (ii) and (iv)
- (i) and (iii)
- (ii) and (iii)
Answer
(i) and (iv)
Reason — Wrapper classes in Java are object representations of primitive data types. Boolean and Character are wrapper classes corresponding to the primitive types boolean and char, respectively.
Conversion of a wrapper class to its corresponding primitive type is known as :
- unboxing
- autoboxing
- type casting
- parsing
Answer
unboxing
Reason — Unboxing is the automatic conversion of an object of a wrapper class (like Integer, Double, etc.) to its corresponding primitive data type (int, double, etc.).
The String method, which results only in a positive integer, is :
- indexOf
- lastIndexOf
- compareTo
- length
Answer
length
Reason — The length() method of the String class returns the number of characters in the string as a positive integer. Other methods like indexOf and lastIndexOf return the position of a character or substring, which can be -1 if not found, and compareTo returns an integer that can be positive, negative, or zero depending on lexicographical comparison.
The method to convert a String to double is:
- String.toDouble()
- Double.Parsedouble()
- Double.parseDouble(String)
- Double.parseDouble()
Answer
Double.parseDouble(String)
Reason — The method parseDouble in the Double class is used to convert a String representing a numeric value into a primitive double type.
The java statement System.out.println(x[x.length]) results in:
- logical error
- syntax error
- run time error
- no error
Answer
run time error
Reason — In Java, arrays are zero-indexed, meaning the valid indices range from 0 to x.length - 1. x.length gives the total number of elements in the array, but accessing x[x.length] tries to access an index outside the valid range, resulting in a ArrayIndexOutOfBoundsException at runtime.
Which of the following is a valid array declaration statement to store the Gender of 80 employees [ 'M'-male, 'F'-female, 'T'-transgender ]?
- char gender = new char[80];
- char gender[] = new char[80];
- char gender[80];
- char gender[80] = new char[ ];
Answer
char gender[] = new char[80];
Reason — In Java, arrays must be declared with the type followed by square brackets [] either before or after the variable name. The correct syntax to declare a character array of size 80 is char gender[] = new char[80];.
Arrange the following java statements in the correct order of execution to accept values into the array a[]:
(i) a[i]=sc.nextInt( );
(ii) int a[]=new int[10];
(iii) Scanner sc=new Scanner(System.in);
(iv) for(i=0;i<10;i++)
- (i), (ii), (iii), (iv)
- (ii), (iii), (iv), (i)
- (iv), (iii), (ii), (i)
- (iii), (i), (iv), (ii)
Answer
(ii), (iii), (iv), (i)
Reason — First, the array a must be declared and initialized (int a[] = new int[10];). Then, the Scanner object sc is created to accept input (Scanner sc = new Scanner(System.in);). Next, a for loop (for(i=0; i < 10; i++)) is used to iterate over the array indices. Inside the loop, values are accepted from the user and stored in the array (a[i] = sc.nextInt();). This sequence ensures proper initialization and input handling.
The statements in the correct order of execution are as follows:
(ii) int a[]=new int[10];
(iii) Scanner sc=new Scanner(System.in);
(iv) for(i=0;i<10;i++)
(i) a[i]=sc.nextInt( );
In the bubble sort technique, during each iteration of the inner loop, two adjacent elements are ............... and ............... .
(i) compared
(ii) swapped
(iii) selected
(iv) deleted
- (i) and (ii)
- (ii) and (iii)
- (iii) and (iv)
- (ii) and (iv)
Answer
(i) and (ii)
Reason — In the bubble sort technique, during each iteration of the inner loop, two adjacent elements are first compared to check their order. If they are found to be in the wrong order, they are swapped to move the larger element towards the end of the list. This process repeats until the array is sorted.
What is the highest index of any array with 100 elements?
- 100
- 101
- 99
- 98
Answer
99
Reason — Arrays are zero-indexed, meaning the first element of the array is at index 0. Therefore, an array with 100 elements has indices ranging from 0 to 99. Hence, the highest index is 99.
Mr. Sanjay is an event manager, he plans and allots duties to each of his subordinates to handle different events. In a program, a task is divided into simple methods. Name the feature used in these contexts.
- Complexity
- Modularity
- Reusability
- Monolithic
Answer
Modularity
Reason — Modularity refers to the practice of dividing a program into separate sub-programs or methods, each handling a specific task. In the context where Mr. Sanjay divides a task into simple methods, the feature being used is modularity.
Raj was asked to accept the phone number which has 10 digits, using the appropriate method of Scanner class. Which of the following statement he must choose?
- Ob.nextInt()
- Ob.nextDouble()
- Ob.nextLong()
- Ob.next().chatAt(0)
Answer
Ob.nextLong()
Reason — Raj must use Ob.nextLong() because a 10-digit phone number exceeds the range of the int data type. The long data type can handle larger values, making it suitable for storing 10-digit numbers.
Raj wanted to count the number of digits in a given number without using a loop. Which of the following statements is correct to perform the above?
- String.valueOf(n).length()
- Integer.parseInt(n).length()
- n.length()
- All the above
Answer
String.valueOf(n).length()
Reason — The expression String.valueOf(n).length() converts the number n into a string using String.valueOf(n), and then calculates the number of characters (digits) in that string using .length(). This is a valid and loop-free way to count digits in an integer.
Sham was asked to encode a string S, by replacing the letter E with #. Select the appropriate statement:
- S.replace('#','E')
- S.replace('E')
- S.replace('E', '#')
- S.replace('#')
Answer
S.replace('E', '#')
Reason — The replace() method in Java is used to replace characters in a string. The correct syntax is S.replace(oldChar, newChar). Since the question asks to replace the letter E with #, we use S.replace('E', '#').
When the String m is compared to n, the result obtained is greater than zero. Which of the following is true?
mcomes before n in the dictionary.ncomes beforemin the dictionary.mandnare equal.mandnhave the same length.
Answer
n comes before m in the dictionary.
Reason — If comparing string m to n results in a value greater than zero, it means m is lexicographically greater than n. Hence, n comes before m in the dictionary.
Which of the following String methods has integer argument?
(i) length
(ii) substring
(iii) indexOf
(iv) charAt
- Only (ii)
- (i) and (iv)
- (ii) and (iv)
- (iii) and (iii)
Answer
(ii) and (iv)
Reason — The substring(int beginIndex) and charAt(int index) methods both take an integer argument to specify a position within the string. The substring method returns a part of the string starting from the given index, while charAt returns the character at the specified index.
Assertion: Property by virtue of which one class acquires the properties of another class is termed as Inheritance.
Reason: Inheritance promotes reusability.
- Assertion is true, Reason is false.
- Assertion is true, Reason is true.
- Assertion is false, Reason is false.
- Assertion is false, Reason is true.
Answer
Assertion is true, Reason is true.
Reason — Inheritance is the property by which one class (subclass) acquires the properties of another class (superclass). It promotes reusability by allowing code to be written once and used by multiple classes.
Which of the following is NOT true for polymorphism?
- All methods have the same name.
- Methods are invoked based on the arguments.
- Methods should have the same number and the same type of arguments.
- It is a principle of OOPs.
Answer
Methods should have the same number and the same type of arguments.
Reason — Polymorphism allows methods with the same name to behave differently based on the number or type of arguments passed. Hence, methods do not need to have the same number and same type of arguments.
Which of the following is a valid initialisation statement?
- int x = "GOOD";
- int y = 45.0/2;
- int z = (int)'x';
- int m = false ;
Answer
int z = (int)'x';
Reason
int x = "GOOD";—"GOOD"is a String literal, not an integer. We cannot assign aStringto a variable of typeint.int y = 45.0/2;—45.0is a double literal, so the result of the division is adouble. Assigning adoublevalue to anintwithout explicit typecasting causes a type mismatch error.int z = (int)'x';— This is a valid initialization because the character'x'is explicitly typecast to an integer, storing its Unicode value in the variablez.int m = false ;—falseis a boolean literal. We cannot assign abooleanvalue to a variable of typeint.
Which of the following is a valid statement to print the following sentence:
Raj said "Good morning"
- System.out.println("Raj said "Good morning");
- System.out.println("Raj said \\Good morning\\);
- System.out.println("Raj said \"Good morning\" ");
- System.out.println("Raj said Good morning");
Answer
System.out.println("Raj said \"Good morning\" ");
Reason — The statement System.out.println("Raj said \"Good morning\" "); is valid because it uses the escape sequence \" to include double quotes inside the string. This ensures the output displays exactly as Raj said "Good morning".
Which data structure is represented in the below picture?

(i) A two-dimensional array with 2 rows and seven columns.
(ii) A one-dimensional array with 14 elements.
- Both (i) and (ii)
- Only (i)
- Only (ii)
- None of the (i) and (ii)
Answer
Only (i)
Reason — A two-dimensional array is a collection of elements arranged in rows and columns. The ice tray shown in the figure looks like a grid with 2 rows and 7 columns, just like a 2D array.
What is the type of looping statement depicted in the below picture?

- Entry controlled loop
- Exit controlled loop
- Multiple branching statement
- All the above.
Answer
Entry controlled loop
Reason — An entry controlled loop checks the condition before executing the loop body. In the figure, the condition is evaluated first, and the commands run only if the condition is true, which matches the behaviour of an entry controlled loop like a while loop.
What is the process done in the below picture?

- Sorting the list in descending order
- Searching the character in the list
- Sorting the list in ascending order.
- None of the above.
Answer
Sorting the list in ascending order.
Reason — The characters in the list are rearranged from an unordered sequence (B, D, A, C) to an ordered sequence (A, B, C, D) following the alphabetical order, which is called sorting in ascending order.
Name the method of search depicted in the below picture.

- Binary Search
- Selection Sort
- Bubble Sort
- Linear Search
Answer
Linear Search
Reason — In Linear Search, each element in the list is checked one by one from the beginning until the required value is found. In the picture, the search starts at the beginning and checks each value one after the other until it finds '6', which matches the method of Linear Search.
Name the feature of Java depicted in the below picture.

- Encapsulation
- Inheritance
- Polymorphism
- Data abstraction
Answer
Polymorphism
Reason — Polymorphism means "many forms." In Java, it refers to the ability of a single object to behave differently in different situations. Just like the same person behaves like a customer in a mall, passenger in a bus, student in school, and son at home.
Name the data types in order from top, given in the above picture.

- int, char, double, String
- String, int, char, double
- char, double, int, String
- int, double, char, String
Answer
String, int, char, double
Reason
- The first value is a sentence in quotes, which is a String.
- The second value is
578, a whole number without decimal, so it is of int type. - The third value is
\n, which is an escape sequence representing a character, hence char type. - The fourth value is
51.048053, a number with decimals, which is of double type.
How many bytes are occupied by the below two-dimensional array?
| 156 | 20 | 154 | 20 |
| 130 | 20 | 130 | 20 |
| 162 | 17.67 | 161 | 20 |
| 158 | 17.5 | 154 | 20 |
- 96 bytes
- 128 bytes
- 12 bytes
- 24 bytes
Answer
128 bytes
Reason — The given table represents a 4×4 two-dimensional array (4 rows and 4 columns), which means there are 16 elements in total. The values include both integers and decimal numbers, indicating that the array is of double data type (which can store both whole numbers and decimal numbers).
In Java, a double occupies 8 bytes.
So, total bytes = 16 elements × 8 bytes = 128 bytes.
A girl wanted to calculate the sum of two numbers stored as a and b multiplied by 7. Select the appropriate Java expression.
- a + b*7
- 7*a + b
- (a + b)*7
- a + 7*b
Answer
(a + b)*7
Reason — To calculate the sum of two numbers stored as a and b multiplied by 7, we use parentheses to add a and b first, then multiply the result by 7. The correct expression is (a + b)*7.
The sum of a[1] and a[3] in the array int a[] = {20, 40, 60, 80, 100} is:
- 80
- 100
- 120
- 60
Answer
120
Reason — In the array int a[] = {20, 40, 60, 80, 100}, the index starts from 0.
a[1]refers to the second element: 40a[3]refers to the fourth element: 80
So, the sum is: 40 + 80 = 120.
State which access specifier is less restrictive.

- private
- protected
- default
- public
Answer
public
Reason — The public access specifier is the least restrictive because it allows access to the class members from any other class, anywhere in the program.
The AMB hotel gives the amount to be paid by the customer as an integer, which of the following Math method rounds off the bill in decimals to an integer?
- Math.round()
- Math.pow()
- Math.ceil()
- Math.abs()
Answer
Math.round()
Reason — The Math.round() method rounds a decimal number to the nearest integer. It returns the closest whole number, which is suitable for rounding off a bill amount to an integer.
Identify which of the following leads to an infinite loop.
- for(i = 10; i != 0; i--)
- for(i = 3; i <= 30; i += 3)
- for(i = 1; i >= 1; i++)
- for(i = 1; i >= 0; i--)
Answer
for(i = 1; i >= 1; i++)
Reason — In the loop for(i = 1; i >= 1; i++), the condition i >= 1 will always be true because i starts at 1 and increases by 1 in each iteration. Since i is always greater than or equal to 1, the loop condition never becomes false. Therefore, the loop runs infinitely, causing an infinite loop.
Assertion: A class can have multiple constructors.
Reason: Multiple constructors are defined with same set of arguments.
- Assertion is true, Reason is false.
- Both assertion and Reason are false.
- Both assertion and Reason are true.
- Assertion is false, Reason is true.
Answer
Assertion is true, Reason is false.
Reason — A class can have multiple constructors, this is called constructor overloading. However, multiple constructors cannot have the same set of arguments because that would cause ambiguity. Each constructor must have a different number or type of parameters.
Assertion: An array can store elements of different data types.
Reason: An array is a user-defined data type with multiple values of the same data type but a different memory index.
- Assertion is true, Reason is false.
- Both assertion and Reason are false.
- Both assertion and Reason are true.
- Assertion is false, Reason is true.
Answer
Assertion is false, Reason is true.
Reason — An array is a user-defined data type that stores multiple values of the same data type at different memory locations. It cannot store elements of different data types.
Which of the following statement is an example for explicit typecasting?
- int amount = 45;
- int amount = 24*24;;
- int amount = Integer.parseInt("45")
- int amount = (int)45.75;
Answer
int amount = (int)45.75;
Reason — Explicit typecasting is when we manually convert one data type to another by specifying the type in parentheses. Here, (int)45.75 converts the double value 45.75 to an int by removing the decimal part.
The output of the statement Math.ceil.(89.9) + Math.floor(90.5) is:
- 0.0
- 180
- 180.0
- 180.4
Answer
180.0
Reason
Math.ceil(89.9)returns the smallest integer greater than or equal to 89.9, which is 90.0.Math.floor(90.5)returns the largest integer less than or equal to 90.5, which is 90.0.- Adding them: 90.0 + 90.0 = 180.0
So, the output is 180.0.
The output of the statement of "TIGER".indexOf('G') is:
- 3
- 2
- -1
- 0
Answer
2
Reason — The indexOf() method in Java returns the position (index) of the first occurrence of the specified character in the string.
In the string "TIGER":
Tis at index 0Iis at index 1Gis at index 2
So, "TIGER".indexOf('G') returns 2.
Choose the statement which is equivalent to the given:
if(a>b)
System.out.println("Honest");
else
System.out.println("Sincere");a>b? System.out.println("Honest"):System.out.println("Sincere");System.out.println(a>b? "Honest":"Sincere");a>b? "Honest": "Sincere";a>b: "Honest"? "Sincere";
Answer
System.out.println(a>b? "Honest":"Sincere");
Reason — The ternary operator condition ? value1 : value2 is used to select one of two values based on a condition. The above if-else statement can be rewritten using the ternary operator inside the println() method as: System.out.println(a > b ? "Honest" : "Sincere");. This statement prints "Honest" if a > b, otherwise it prints "Sincere".
The two Java statement are used to check for the equality of the strings "COP" and "cop" are as follows:
"COP".equals("cop")
"COP".equalsIgnoreCase("cop")The output of the above statements is:
- false, true
- true, false
- false, false
- true, true
Answer
false, true
Reason — In Java:
"COP".equals("cop")checks for exact match, including uppercase and lowercase letters. Since"COP"and"cop"are not exactly the same, it returns false."COP".equalsIgnoreCase("cop")checks for equality by ignoring the case of letters. So it treats"COP"and"cop"as equal and returns true.
The output of the java statement "SOLDIER".compareTo("SOLUTE"); is
- -4
- -17
- 17
- 0
Answer
-17
Reason — The compareTo() method in Java compares two strings lexicographically using the Unicode values.
Let's compare "SOLDIER" and "SOLUTE":
- The first three letters S, O, L are the same in both strings.
- The 4th letter in "SOLDIER" is 'D' and in "SOLUTE" it is 'U'.
- Unicode of 'D' = 68, Unicode of 'U' = 85.
- 68 − 85 = -17
So, the output is -17.
Arrange the following in the descending order of number of bytes occupied.
(i) char x[20]
(ii) int a[4][2]
(iii) double b[6]
- (iii), (ii), (i)
- (iii), (i), (ii)
- (ii), (iii), (i)
- (i), (ii), (iii)
Answer
(iii), (i), (ii)
Reason — To find the number of bytes occupied by each array, we multiply the number of elements by the size of each data type:
(i) char x[20] — 20 elements * 2 bytes = 40 bytes
(ii) int a[4][2] — 4 * 2 = 8 elements * 4 bytes = 32 bytes
(iii) double b[6] — 6 elements * 8 bytes = 48 bytes
Now arranging in descending order: (iii) 48 bytes, (i) 40 bytes, (ii) 32 bytes
Sam performs bubble sort on the following array to organise the elements in ascending order:
{5, 1, 2, 3}
After the first comparison the array is:
{1, 5, 2, 3}
What would be the array after the next comparison?
- {1, 2, 5, 3}
- {1, 5, 3, 2}
- {1, 3, 5, 2}
- {1, 3, 2, 5}
Answer
{1, 2, 5, 3}
Reason — In Bubble Sort, adjacent elements are compared and swapped if they are in the wrong order.
Original array: {5, 1, 2, 3}
After the first comparison (compare 5 and 1):
Since 5 > 1, they are swapped → {1, 5, 2, 3}
Next comparison (second comparison):
Compare 5 and 2. Since 5 > 2, they are swapped → {1, 2, 5, 3}
So, after the next comparison, the array becomes {1, 2, 5, 3}.