This collection of ICSE Class 10 Computer Applications MCQs provides a crucial testing ground for your understanding of core Java programming concepts. Taken from the Class 10 – ICSE Computer Applications Solved Competency Questions book, this chapter on Multiple Choice Questions helps you quickly assess your knowledge on topics like method signatures, parameter passing, reusability, and different types of programming errors. Each question is designed to mimic the format you will encounter in Section A of your board exam, making this an essential practice set for solidifying your theoretical foundation before you tackle more complex programming problems. Itβs a perfect way to revise key definitions and principles in a targeted, exam-oriented format.
You’ve likely landed here searching for correct answers and clear explanations for the competency-based questions in your textbook. This page provides exactly that. We have solved all 47 Multiple Choice Questions from this chapter, ensuring each solution not only gives the correct option but also explains the underlying concept as per the ICSE curriculum. You won’t just find out what the right answer is, but also why it is correct. Here you will find reliable, step-by-step verified solutions to help you ace your exams.
Multiple Choice Questions (1 Mark Each)
Question 1
What is the type of error, if any, when two methods have the same method signature?
- Runtime error
- Logical error
- Syntax error
- No error
The correct answer is Syntax error.
If you have two methods in the same class with the very same method signature, the compiler gets confused. It’s like having two books with the exact same title and author in the same spot on a shelf. When you ask for the book, which one should the librarian give you? The compiler faces the same problem; it cannot differentiate which method to call. Because this breaks a fundamental rule of the Java language, the compiler will stop and report a Syntax error before the program can even be run.
Question 2
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)
The correct answer here points to (i) and (iii). Let’s think about why.
First, user-defined methods promote reusability. Imagine you need to perform the same calculation at several different points in your code. Instead of typing out the same lines over and over again, you can package them into a single method. This means the code is written once and used multiple times, which is a fantastic way of reducing redundancy.
Second, they support modularity. A large program can get complicated very quickly! Methods allow us to tackle this by breaking a program into smaller, manageable, and logically separate units. Think of it like organising a big project into smaller, distinct tasks. When each method has its own specific job, the entire program becomes so much easier to understand, maintain, and debug.
Question 3
Parameters which receive the values from the called method are termed as …………… parameters.
- actual
- formal
- reference
- class
Let’s think about how methods communicate. When you write a method, you define a list of variables in its signature to accept incoming data. These variables are called formal parameters.
Think of them as placeholders, or empty slots, waiting in the method definition. When another part of your codeβthe calling methodβactually uses your method, it sends real values to it. These real values are the actual parameters. The formal parameters are the ones that receive the actual values during this process, which we call method invocation.
Question 4
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)
Think of a method prototype, or method signature, as its official declaration. Itβs a strict contract with the Java compiler, and it must provide four key pieces of information in a specific order.
- The access modifier (like
public), which controls the visibility of the method. - The return type (here, it’s
int), which declares the data type of the value the method will send back once its job is done. - The method name (
perform), which is how you will call or invoke the method. - The parameter list, enclosed in parentheses. This list,
(int a, int b), specifies the data that the method needs to receive to work. It’s crucial to remember that parameters are always separated by commas.
The statement public int perform (int a, int b) correctly follows this required syntax, making it the only valid prototype among the choices.
Question 5
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’);
Let’s look closely at the method’s prototype: int display(int a, char ch). Think of this as a strict set of instructions. It tells us that to call the method display, we absolutely must provide two arguments, and they have to be in a specific order.
int and the second argument must be a char. When we examine the statement int m = display(45,'A');, we can see it follows these rules perfectly. The value 45 is an integer, and 'A' is a character literal, passed in the correct sequence. This is why it’s the correct way to invoke the method.Question 6
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”);
Let’s break down how this works in Java. Remember, a string like "RESPECT" is treated as an object. Objects have special built-in functions, called methods, that can perform actions on them.
toLowerCase(). It’s crucial to get the name exactly right because Java is a case-sensitive language. This means toLowerCase() is correct, while something like tolowercase() would cause an error. The method is always called on the string object itself.Therefore, the correct way to write the statement is to start with the string object "RESPECT", followed by a dot, and then the method call. This gives us the correct syntax: "RESPECT".toLowerCase( );.
Question 7
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)
Let’s think about this carefully. In Java, you’re already familiar with our fundamental data types, like boolean and char. We call these primitive data types.
Now, sometimes we need to treat these simple values as if they were proper objects. This is where Wrapper classes come into play. They essentially ‘wrap’ a primitive value inside an object, giving it more capabilities.
A simple way to spot them is by the naming convention: primitive types are always in lowercase, while their corresponding Wrapper classes start with a capital letter.
- The wrapper class for the primitive type
booleanisBoolean. - The wrapper class for the primitive type
charisCharacter.
This makes (i) and (iv) the correct choices.
Question 8
Conversion of a wrapper class to its corresponding primitive type is known as :
- unboxing
- autoboxing
- type casting
- parsing
unboxing
Integer or Double, and you need its simple, underlying value, Java performs an unboxing. It’s the automatic conversion of that wrapper object back to its corresponding primitive data type. So, an Integer object gets ‘unboxed’ into an int, and a Double object becomes a double.Question 9
The String method, which results only in a positive integer, is :
- indexOf
- lastIndexOf
- compareTo
- length
This is a great question that really tests our understanding of what these common methods return.
length() method is for. Its only job is to count and return the number of characters in a string. Since a string can’t have a negative number of characters, the length() method of the String class will always give you a positive integer.Now, let’s look at the other options to be sure:
– The methods indexOf and lastIndexOf are used to find the position of a character or substring. If they find what they’re looking for, they return a positive number (or zero). But what if the character isn’t in the string at all? In that case, they return -1 to signal that it was not found.
– The compareTo method is different again. It’s used for lexicographical comparison (like in a dictionary). It returns an integer that can be positive, negative, or zero, depending on how the two strings compare to each other.
So, you can see that only length() is guaranteed to always result in a positive integer.
Question 10
The method to convert a String to double is:
- String.toDouble()
- Double.Parsedouble()
- Double.parseDouble(String)
- Double.parseDouble()
Remember the wrapper classes we’ve talked about? For the primitive double type, its powerful helper is the Double class. This class contains a special toolkit of methods, and the one we need here is parseDouble.
So, the correct syntax is Double.parseDouble(String). This is telling Java to go into the Double class, find the parseDouble method, and use it to convert a String that represents a numeric value into a primitive double type that you can actually use for calculations.
Question 11
The java statement System.out.println(x[x.length]) results in:
- logical error
- syntax error
- run time error
- no error
Think about how we label positions in an array. It’s a crucial concept in Java. Arrays are zero-indexed, which is a way of saying that the counting always starts from 0.
So, for any array named x, the very first element is at index 0, the second is at index 1, and so on. This means the last valid index is always one less than the total number of elements. The rule is that valid indices for array x will always range from 0 to x.length - 1.
The expression x.length itself gives you the total count of elements. If an array has 5 elements, x.length is 5, but its valid indices are 0, 1, 2, 3, and 4.
The statement x[x.length] attempts to access the element at the index that is equal to the length of the array. As we just saw, this index is always one position beyond the array’s boundary. Trying to access an element that doesn’t exist is an error that the computer can only detect while the program is actually running.
This kind of error is known as a run time error. Specifically, Java will stop the program and report an ArrayIndexOutOfBoundsException.
Question 12
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[ ];
The correct statement to create this array is char gender[] = new char[80];.
Java as a two-part instruction. First, you declare it. The char gender[] part tells Java, “I’m planning to have an array of characters called gender.” The square brackets [] are essential; they’re what makes it an array. Second, you have to actually create it in memory. The new char[80]; part does this, reserving exactly 80 spots for character data. Putting it all together gives you the complete, correct syntax: char gender[] = new char[80];.Question 13
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)
Let’s think about this logically, just like the computer has to. Before you can put things into a set of boxes, you first need to have the boxes, right?
a that can hold 10 integers.” That is exactly what statement (ii) int a[]=new int[10]; does. It builds our empty container.Once we have our container, we need a way to get the items to put inside it. To accept values from the user’s keyboard, we need a special tool. In Java, that tool is the Scanner object. So, our next logical step is to create it with (iii) Scanner sc=new Scanner(System.in);.
Now we have our array a and our input tool sc. We need to fill each of the 10 slots in the array, from index 0 to 9. A for loop is the perfect way to repeat an action a specific number of times. Statement (iv) for(i=0;i<10;i++) sets up this repetition, making sure we visit every single spot in the array.
Finally, what is the action that we need to repeat? It’s the process of reading a number and storing it. This happens inside the loop. Statement (i) a[i]=sc.nextInt( ); uses our scanner tool (sc) to get an integer and places it into the current array position (a[i]).
Therefore, the only sequence that makes sense is 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( );
Question 14
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)
(i) and (ii)
During each pass of this loop in the bubble sort technique, the program looks at pairs of adjacent elements. First, these two neighbours are compared. If they are in the wrong order (for instance, the larger number is before the smaller one in an ascending sort), they are immediately swapped. This simple ‘compare and swap’ routine is repeated for every pair, which ensures the largest element in that pass moves to its correct final position. Therefore, the two fundamental operations are comparing and swapping.
Question 15
What is the highest index of any array with 100 elements?
- 100
- 101
- 99
- 98
99
Because of this, for an array that holds 100 elements, the indices will range from 0 to 99. If you count all the numbers from 0 to 99, you’ll find there are exactly 100 positions. Therefore, the highest index you can access in that array is 99.
Question 16
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
Think about how Mr. Sanjay is managing his big event. He’s not trying to do everything himself, right? He’s breaking the large, complex job into smaller, more manageable duties for his team. This is a very smart way to work, and it’s a core concept in programming too.
Question 17
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)
The correct choice here is Ob.nextLong().
int data type, has a maximum limit, and a 10-digit phone number often exceeds this range. It’s like trying to fit a litre of water into a small glass β it just won’t work! For these larger whole numbers, Java provides the long data type, which has a much larger capacity. Therefore, to accept a value that will be stored as a long, Raj must use the corresponding Scanner class method, which is Ob.nextLong().Question 18
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
The correct statement is String.valueOf(n).length().
The first part of the expression,
String.valueOf(n), is a standard Java method that takes the number stored in the variablenand converts it into its text or String equivalent. For instance, ifnholds the integer12345,String.valueOf(n)will produce the string"12345".The second part,
.length(), is a method that can be used on any String. It simply counts the number of characters in that string.
So, when you put them together as String.valueOf(n).length(), you are first converting the number to a string and then immediately finding its length. This gives you the count of the digits without needing to write a single loop.
Question 19
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(‘#’)
Let’s think about the task here. We need to find every instance of one character in a string and swap it with another. For this exact purpose, Java provides the replace() method.
S.replace(oldChar, newChar). You always put the character you want to get rid of first, and the character you want to put in its place second. Since the question asks Sham to replace the letter E with #, our oldChar is 'E' and our newChar is '#'. Following that rule, the correct statement is S.replace('E', '#').Question 20
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.
Think about how words are arranged in a dictionary. Java’s string comparison works on the very same principle. When comparing string m to n gives a result that is greater than zero, it’s a specific signal.
m, is considered ‘larger’ or comes later in alphabetical order than the second string, n. The technical term for this is that m is lexicographically greater than n. If m comes after n in the dictionary, it logically follows that n comes before m in the dictionary.Question 21
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)
When you want to work with a specific part of a string, you need to tell Java exactly where to look. You do this by providing an index, which is always an integer.
substring(int beginIndex) and charAt(int index) require an integer argument to function. You have to give them a number to specify a position inside the string.The substring method uses that integer to know where to start creating a smaller piece of the original string. The charAt method uses the integer you provide to go to that exact spot and return the single character it finds there.
Question 22
Assertion: Property by virtue of which one class acquires the properties of another class is termed as Inheritance.
- Assertion is true, Reason is false.
- Assertion is true, Reason is true.
- Assertion is false, Reason is false.
- Assertion is false, Reason is true.
Both the Assertion and the Reason given here are absolutely correct, and it’s important to see how they connect.
The Assertion perfectly defines Inheritance: it is the property by which one class (which we call the subclass) acquires the properties of another class (the superclass). This is one of the most powerful ideas in object-oriented programming.
Now, why is this so useful? The Reason tells us exactly why: Inheritance promotes reusability. Because the subclass can use the features of the superclass, we don’t have to write the same code over and over again. This means code can be written once and used by multiple classes, which saves a lot of time and helps keep our programs organised.
Question 23
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.
Methods should have the same number and the same type of arguments.
Question 24
Which of the following is a valid initialisation statement?
- int x = “GOOD”;
- int y = 45.0/2;
- int z = (int)’x’;
- int m = false ;
Let’s break down why only one of these statements works correctly. It all comes down to understanding Java’s strict rules about data types.
int x = "GOOD";β Remember, anything inside double quotes like"GOOD"is aStringliteral. You’re trying to put a word into a variable container,int x, that’s designed to hold only whole numbers. This is a mismatch, so it’s not allowed.int y = 45.0/2;β Pay close attention to the number45.0. That.0tells Java it’s adouble(a number with a decimal point). When you perform a calculation involving adouble, the result is also adouble. So, you’re attempting to store a decimal value into anintvariable, which can only hold whole numbers. Without explicitly telling Java how to convert it (which is called typecasting), this will cause a type mismatch error.int z = (int)'x';β This one is perfectly valid! The value'x'is a character literal. The(int)in front of it is an explicit typecast, which tells the computer, “Convert this character into its integer equivalent.” Every character has a corresponding number in the computer’s memory, its Unicode value. This statement calculates that number and stores it in the integer variablez.int m = false ;β The valuefalseis abooleanliteral. It can only betrueorfalse. In Java, abooleanis a completely separate data type from anint. You cannot assign abooleanvalue to a variable of typeint.
Question 25
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”);
Think about how Java understands a string inside System.out.println(). It sees the first double quote and knows the text is starting, and it looks for the next double quote to know when it ends. This creates a small puzzle: how do we print a double quote mark itself?
This is precisely why we have escape sequences. The statement System.out.println("Raj said \"Good morning\" "); is the correct and valid way to do this.
\", is a special instruction to the Java compiler. It essentially says, “Don’t treat this next character as code; treat it as something to be printed literally.” By using this escape sequence, you ensure the output on the screen is exactly Raj said "Good morning".Question 26
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)
Let’s connect this picture to what we’ve learned about arrays. Remember how we describe a two-dimensional array? We often call it a collection of elements arranged in a grid, with distinct rows and columns, much like a table in your notebook.
Now, look closely at the ice tray shown in the figure. It’s a perfect real-world example of this! It isn’t a single line of compartments. Instead, it’s a grid with 2 rows and 7 columns. This grid-like structure is exactly how we visualize a 2D array. Therefore, the description in (i) is the correct one.
Question 27
What is the type of looping statement depicted in the below picture?


- Entry controlled loop
- Exit controlled loop
- Multiple branching statement
- All the above.
Entry controlled loop
while loop.Question 28
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.
Let’s carefully observe what has happened to the characters in the list. Initially, we are given an unordered sequence: (B, D, A, C). After the process is complete, these same characters are rearranged into a perfectly ordered sequence: (A, B, C, D).
This new arrangement simply follows the familiar alphabetical order. In programming, when we take a jumbled list and arrange its elements from the smallest to the largest value (or A to Z, in this case), the process is known as sorting in ascending order.
Question 29
Name the method of search depicted in the below picture.


- Binary Search
- Selection Sort
- Bubble Sort
- Linear Search
Linear Search
Question 30
Name the feature of Java depicted in the below picture.


- Encapsulation
- Inheritance
- Polymorphism
- Data abstraction
The feature being shown here is Polymorphism.
Question 31
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
Let’s look at each value from top to bottom, just as they are presented.
- The first value is an entire sentence wrapped in double quotes. In Java, any sequence of characters inside double quotes is treated as a String.
- Next, we have
578. This is a simple whole number, without any decimal point. The data type for whole numbers like this is int. - The third one,
\n, is a special case. It might look like two characters, but it is an escape sequence that represents a single character: the ‘newline’ character. Because it stands for a single conceptual character, its data type is char. - Lastly, you see the number
51.048053. The presence of a decimal point is the main clue here. For numbers with fractional parts, Java uses the double data type by default.
Question 32
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
Let’s take a close look at the data presented in the table. We can see it’s organized neatly into 4 rows and 4 columns. This structure is a classic example of a 4Γ4 two-dimensional array. A quick multiplication tells us that it holds a total of 16 elements.
Now, what kind of data is it holding? Notice that we have a mix of values. There are whole numbers, but there are also decimal numbers like 17.67 and 17.5. In Java, an array can only have one data type for all its elements. To accommodate both whole numbers and these decimal numbers, the array must be declared as a double data type.
The final piece of the puzzle is knowing how much memory a double uses. You’ll recall that in Java, a single double value occupies 8 bytes in memory.
So, to find the total memory used, we just multiply the number of elements by the size of each element:
Total bytes = 16 elements Γ 8 bytes = 128 bytes.
Question 33
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
Think about the order of operations here, just like you do in Maths. The problem asks you to calculate the sum of the two numbers, a and b, first. Only after you have that sum should you multiply the entire result by 7.
To make sure the computer performs the addition before the multiplication, we must use parentheses. When we write (a + b), we are giving a clear instruction to solve the part inside the brackets first.
Once that sum is calculated, the result is then multiplied by 7. This is how we arrive at the correct Java expression: (a + b)*7.
Question 34
The sum of a[1] and a[3] in the array int a[] = {20, 40, 60, 80, 100} is:
- 80
- 100
- 120
- 60
Let’s take a close look at the given array: int a[] = {20, 40, 60, 80, 100}.
The most important rule to remember for arrays in programming is that the index always starts from 0. It’s a small detail that makes a big difference!
Thinking about it that way, we can see:
– a[1] is not the first element, but refers to the second element, which is 40.
– a[3] is not the third element, but refers to the fourth element, which is 80.
So, to find the sum, we just need to add these two values together.
The sum is: 40 + 80 = 120.
Question 35
State which access specifier is less restrictive.


- private
- protected
- default
- public
When we talk about how ‘restrictive’ an access specifier is, we’re really just asking, ‘How much does it hide our code from other parts of the program?’ The answer here is public because it does the least amount of hiding.
public access specifier is the least restrictive because it makes class members completely visible and available. It allows access to the class members from any other class, anywhere in the program, with no limitations at all.Question 36
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()
Math.round()
Math.round() method is built to do. It takes a decimal number and rounds it to the nearest integer. This makes it the perfect and most logical choice for rounding off a bill amount to an integer.Question 37
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–)
for(i = 1; i >= 1; i++)
for(i = 1; i >= 1; i++). The variable i is initialized to 1. The loop will continue as long as the condition i >= 1 is true. In the very first check, 1 >= 1 is true, so the loop’s body executes.After that first run, the update statement i++ runs, which means i becomes 2. Now, for the second iteration, the condition is checked again: is 2 >= 1? Yes, it is. So the loop runs again. Then i becomes 3, then 4, and it will just keep increasing.
Since i starts at 1 and only increases by 1 in each iteration, its value will always be greater than or equal to 1. The loop condition never becomes false. Therefore, the loop runs infinitely, causing an infinite loop.
Question 38
Assertion: A class can have multiple constructors.
- Assertion is true, Reason is false.
- Both assertion and Reason are false.
- Both assertion and Reason are true.
- Assertion is false, Reason is true.
Let’s carefully examine both statements. The initial Assertion is true, Reason is false.
Now, why is that? A class can most certainly have multiple constructors. Think of it like having different ways to build the same type of object. This powerful feature is called constructor overloading. So, the assertion is perfectly correct.
However, the Reason given is flawed. It suggests that these multiple constructors would have the same set of arguments. If that were true, how would the Java compiler know which constructor to call when you create an object? It would lead to confusion, or what we call ambiguity. To prevent this, Java has a strict rule: Each constructor must have a different number or type of parameters. This unique signature is how Java can distinguish between them.
Question 39
Assertion: An array can store elements of different data types.
- Assertion is true, Reason is false.
- Both assertion and Reason are false.
- Both assertion and Reason are true.
- Assertion is false, Reason is true.
Let’s think about the very nature of an array. Imagine you have a box specifically designed to hold cricket balls. You can put many cricket balls in it, but you can’t suddenly decide to store a football in one of the slots. An array works exactly like that. It’s a collection, but a very strict one. This means the initial Assertion is incorrect because an array cannot store elements of different data types.
Now, let’s examine the second statement, the Reason. It describes an array as a user-defined data type that stores multiple values of the same data type at different memory locations. This is a perfect definition! It correctly highlights that all elements must be of the same data type, which is the very rule the Assertion broke. Each element gets its own unique spot in memory, which we access using its index.
So, when we put it all together, we see that the Assertion is false, Reason is true.
Question 40
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;
int amount = (int)45.75;
(int)45.75 is our direct command; it converts the double value 45.75 to an int. To make this happen, the system simply removes the decimal part.Question 41
The output of the statement Math.ceil.(89.9) + Math.floor(90.5) is:
- 0.0
- 180
- 180.0
- 180.4
Let’s break this expression down into its two parts. It’s much simpler that way!
First, we have
Math.ceil(89.9). Remember,ceilstands for “ceiling,” so you always round up to the next whole number, no matter how small the decimal part is. The smallest integer that is greater than or equal to 89.9 is 90. These functions return a double, so the result is90.0.Next up is
Math.floor(90.5). As you might guess,floormeans we always round down to the integer part. For 90.5, the largest integer less than or equal to it is 90. This also gives us90.0.Now, we just need to perform the addition:
90.0 + 90.0 = 180.0.
So, the final output is 180.0.
Question 42
The output of the statement of "TIGER".indexOf('G') is:
- 3
- 2
- -1
- 0
Think of the indexOf() method in Java as a simple search function. Its job is to return the position, or index, of the first time a specific character appears in a string. The most crucial thing to remember, as always in Java, is that we start counting from 0, not 1.
Let’s map out the string "TIGER":
Tis at index0Iis at index1Gis at index2
When we ask the program for "TIGER".indexOf('G'), it finds the character 'G' at the position numbered 2 and returns that value.
Question 43
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";
Let’s think about what the original if-else statement is doing. It’s making a choice. Based on one single condition, it performs one of two very similar actions: it either prints one word or another.
Java gives us a wonderfully compact tool for this exact kind of ‘either-or’ decision: the ternary operator. Its structure is always condition ? value1 : value2. It evaluates the condition; if it’s true, the whole expression becomes value1, and if it’s false, it becomes value2.
Since the action in both the if and else blocks is the sameβa println() method callβwe can use the ternary operator to decide which string gets sent to be printed. This is much more efficient.
This brings us to the most elegant equivalent statement: System.out.println(a > b ? "Honest" : "Sincere");.
Here, the expression a > b ? "Honest" : "Sincere" gets evaluated first. It resolves to either the string "Honest" (if a > b is true) or the string "Sincere" (if a > b is false). That single resulting string is what finally gets printed to the screen.
Question 44
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
Ah, a classic Java string comparison question! Let’s break down why we get these specific results. It all comes down to how each method treats uppercase and lowercase letters.
The first statement,
"COP".equals("cop"), uses the.equals()method. Think of this as the strictest possible check. It looks for an exact match, character for character, including their case. Since"COP"is in uppercase and"cop"is in lowercase, they are not identical in Java’s eyes. Therefore, it returnsfalse.The second statement,
"COP".equalsIgnoreCase("cop"), gives us a big clue right in its name! This method checks for equality by completely ignoring the case of the letters. It only cares if the letters themselves are the same. In this situation, it treats"COP"and"cop"as equal, so it returnstrue.
Putting it all together, the final output is false, true.
Question 45
The output of the java statement "SOLDIER".compareTo("SOLUTE"); is
- -4
- -17
- 17
- 0
The compareTo() method in Java works a lot like looking up words in a dictionary. It compares two strings lexicographically, which is just a formal way of saying it goes character-by-character using their Unicode values until it finds a difference.
Let’s trace the comparison for "SOLDIER" and "SOLUTE":
- The first three letters, S, O, and L, are exactly the same in both strings, so the check moves on.
- The first point of difference happens at the 4th letter. In
"SOLDIER"we have ‘D’, and in"SOLUTE"we have ‘U’. - The moment a difference is found, the comparison stops. The method then calculates the difference between the Unicode values of these two characters.
- The Unicode of ‘D’ is 68, and the Unicode of ‘U’ is 85.
- The final result is the value from the first string minus the value from the second:
68 β 85 = -17.
So, the output is -17.
Question 46
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)
To arrange these arrays from largest to smallest, we first need to figure out exactly how much memory each one occupies. The trick is to multiply the total number of elements in the array by the size of its specific data type.
Let’s calculate them one by one:
For (i) char x[20], we have an array with 20 elements. Each
charin Java takes up 2 bytes. So, the total space is20 elements * 2 bytes = 40 bytes.Next is (ii) int a[4][2]. This is a 2D array. To find the total number of elements, we simply multiply the dimensions:
4 * 2 = 8 elements. Anintvalue requires 4 bytes of memory. Therefore, the total size is8 elements * 4 bytes = 32 bytes.Finally, for (iii) double b[6], we have 6 elements. The
doubledata type, used for larger decimal numbers, takes up 8 bytes. This gives us a total of6 elements * 8 bytes = 48 bytes.
Now we can easily arrange them in descending order (from biggest to smallest):
(iii) is 48 bytes, (i) is 40 bytes, and (ii) is 32 bytes.
So, the final order is (iii), (i), (ii).
Question 47
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}
Let’s trace the movement of the elements carefully. The core idea of Bubble Sort is that we compare adjacent, or side-by-side, elements and swap them if they are in the wrong order.
Our original array is: {5, 1, 2, 3}
First Comparison: We compare the first two elements,
5and1. Since5 > 1and we want ascending order, they are swapped. This gives us the array mentioned in the problem:{1, 5, 2, 3}.Next Comparison: Now, this is the key part. We continue from where we left off, using the new state of the array. We move to the next pair of adjacent elements in
{1, 5, 2, 3}, which are5and2. We compare them. Since5 > 2, they are also in the wrong order and must be swapped.
After this swap, the array becomes {1, 2, 5, 3}.
So, after the next comparison, this is what the array looks like.