1.
IsNaN is made available in
Correct Answer
C. double
Explanation
The correct answer is "double" because the isNaN function is made available in the double data type. The isNaN function is used to determine whether a value is NaN (Not-a-Number) or not. The double data type represents double-precision floating-point numbers in Java, which can store very large or very small numbers with decimal points. Therefore, the isNaN function can be used to check if a double value is a valid number or not.
2.
The three most important methods available in the wrapper classes are:
Correct Answer(s)
A. XxxValue()
C. ParseXxx()
E. ValueOf()
Explanation
The wrapper classes in Java provide methods to convert primitive data types to their corresponding wrapper objects and vice versa. The three most important methods available in the wrapper classes are xxxValue(), parseXxx(), and valueOf().
The xxxValue() method is used to convert a wrapper object to its corresponding primitive data type. For example, intValue() converts an Integer object to an int.
The parseXxx() method is used to convert a string representation of a primitive data type to its corresponding wrapper object. For example, parseInt() converts a string to an Integer object.
The valueOf() method is used to convert a string representation of a primitive data type to its corresponding wrapper object. It is similar to the parseXxx() method but returns a cached instance if available, which can improve performance.
These methods are essential for converting between primitive data types and their wrapper objects.
3.
Read the following program
class test
{
public static void main(String [] args)
{
int x = 3;
int y = 1;
if (x = y)
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
What is the result?
Correct Answer
C. An error at " if (x = y)" causes compilation to fall.
Explanation
The given program contains a mistake in the if statement. Instead of using the equality operator (==), the assignment operator (=) is used. This means that the value of y is assigned to x, and the if statement will always evaluate to true. As a result, the program will not compile and will throw a compilation error.
4.
What is the output?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}
Correct Answer
B. Tiny
Explanation
The output of the given code is "Tiny". This is because the ternary operator is used to evaluate the value of the variable "sup". The condition (x < 15) is false, so it moves to the next condition (x < 22), which is true since x = 20. Therefore, the value assigned to "sup" is "tiny" and it is printed as the output.
5.
Primitive data types
Correct Answer(s)
B. Are not objects
C. Cannot access methods of object class
Explanation
Primitive data types are not objects because they do not have the characteristics of objects, such as having methods and being able to be manipulated using object-oriented programming concepts. Primitive data types are basic data types that are built into the programming language and are used to store simple values like numbers or characters. They are not instances of a class and therefore cannot access methods of the object class, which is a fundamental class in object-oriented programming.
6.
Which of the following are legal identifiers
Correct Answer(s)
B. Variable2
C. _whatavariable
D. _3_
E. $anothervar
Explanation
Legal identifiers in programming are names used to identify variables, functions, or other entities. They must follow certain rules, such as starting with a letter or underscore, and can contain letters, numbers, or underscores. In this question, the identifiers "variable2", "_whatavariable", "_3_", and "$anothervar" all follow these rules and are therefore legal identifiers. On the other hand, "2variable", "#myvar", and "variable2" are not legal identifiers because they start with a number or a special character.
7.
Class train
{
public static void main(String[] args)
{
Integer x = 0;
int y= 0;
for(int z = 0; z < 5; z++)
if ((++x > 2) || (++y>1))
x++;
System.out.println(x + " " + y);
}
}
What is the output of the following program?
Correct Answer
C. 9 2
Explanation
The program initializes two variables, x and y, to 0. It then enters a for loop that iterates 5 times. Inside the loop, there is an if statement that checks two conditions. The first condition is (++x > 2), which increments x by 1 and checks if it is greater than 2. The second condition is (++y > 1), which increments y by 1 and checks if it is greater than 1. If either of these conditions is true, x is incremented by 1. After the if statement, the program prints the values of x and y.
In the first iteration of the loop, x is incremented to 1 and y remains 0, so the output is "1 0". In the second iteration, x is incremented to 2 and y is incremented to 1, so the output is "2 1". In the third iteration, x is incremented to 3 and y is incremented to 2, so the output is "3 2". In the fourth and fifth iterations, x remains 3 and y remains 2, so the output is "3 2" again. Therefore, the correct answer is "9 2".
8.
The default keyword can be located anywhere in the
Correct Answer
A. Switch block
Explanation
The default keyword can be located anywhere in the switch block. This means that it can be placed within the switch statement, which is the main part of the switch construct. The switch block contains multiple case statements and the default keyword is used to specify the code that should be executed if none of the case statements match the switch expression. By allowing the default keyword to be located anywhere within the switch block, it provides flexibility in organizing the code and makes it easier to understand and maintain.
9.
Wrapper classes are -----------
Correct Answer
B. Immutable
Explanation
Wrapper classes in Java, such as Integer, Double, and Boolean, are immutable. This means that once an object of a wrapper class is created, its value cannot be changed. Any operation that appears to modify the value actually creates a new object. This immutability ensures that the values stored in wrapper classes are consistent and cannot be accidentally modified, providing a level of safety and predictability in Java programs.
10.
The easiest way to turn a number into a string is to simply concatenate using
Correct Answer
A. +
Explanation
The easiest way to turn a number into a string is to simply concatenate using the "+" operator. This operator is used to combine or join two strings together. In this case, by using the "+" operator, the number can be converted into a string by appending it to an empty string. This will result in the number being converted into a string and can be used or displayed as such.
11.
What is the output of System.out.println(-3>>>1)?
Correct Answer
A. 2147483646
Explanation
The output of System.out.println(-3>>>1) is 2147483646. The ">>>" operator is the unsigned right shift operator in Java. When a negative number is right-shifted using the unsigned right shift operator, the sign bit is always set to 0, resulting in a positive number. In this case, -3 is represented in binary as 11111111111111111111111111111101. When we right-shift this number by 1 position using the unsigned right shift operator, we get 01111111111111111111111111111110, which is equivalent to 2147483646 in decimal.
12.
double c=Double.parseDouble("50h");
System.out.println(c);
What is the output of the above program?
Correct Answer
C. This results in runtime error 'Number Format Exception'
Explanation
The given program tries to parse the string "50h" into a double using the Double.parseDouble() method. However, since the string contains a non-numeric character ('h'), it cannot be converted into a valid number format. As a result, a NumberFormatException is thrown at runtime, causing the program to terminate with an error.
13.
boolean b;
System.out.println(b);
What is the output of the above program?
Correct Answer
B. False
Explanation
The output of the above program is "False" because the variable "b" is not initialized with any value. In Java, boolean variables have a default value of "false" if not explicitly assigned a value. Therefore, when the program tries to print the value of "b", it will output "false".
14.
Which will legally declare, construct, and initialize an array?
Correct Answer
D. Int myList [] = {4, 3, 7};
Explanation
The correct answer is "int myList [] = {4, 3, 7}." This statement declares, constructs, and initializes an array named "myList" of type int. The array contains three elements: 4, 3, and 7.
15.
What gets printed when the following program is compiled and run. Select the one correct answer.
class test {
public static void main(String args[]) {
int i,j,k,l=0;
k = l++;
j = ++k;
i = j++;
System.out.println(i);
}
}
Correct Answer
B. 1
Explanation
The program initializes four variables i, j, k, and l to 0. The value of l is then assigned to k using the post-increment operator, which means k will be assigned the current value of l (0) and then l will be incremented by 1. Next, k is assigned to j using the pre-increment operator, which means j will be assigned the incremented value of k (1). Finally, j is assigned to i using the post-increment operator, which means i will be assigned the current value of j (1) and then j will be incremented by 1. Therefore, the value of i is 1, which is printed to the console.