1.
Following code will compile.
public class TestOnOprs {
public static void main(String[] args) {
float f = 36.25;
System.out.println(f);
}
}
Correct Answer
B. False
Explanation
The given code will not compile because there is a type mismatch error. The variable "f" is declared as a float, but the value assigned to it (36.25) is a double. To fix this error, the value should be explicitly cast to a float by adding an "f" at the end: float f = 36.25f;
2.
What is easiest way to assign the value to an integer for the given declaration? (Choose One)
char x = 'Y';
Correct Answer
C. Int i = c;
Explanation
The easiest way to assign the value to an integer for the given declaration is by using the statement "int i = c;". This directly assigns the value of the variable "c" to the integer variable "i" without any need for type casting or conversion.
3.
What will be the result of the following code? (Choose One)
public class TestAssignment {
public static void main(String[] args) {
int x,y,z;
z = 100;
x = y = z = 130;
System.out.println(y);
}
}
Correct Answer
B. Prints 130
Explanation
The code assigns the value 130 to the variables x, y, and z in a single line. Therefore, when the code prints the value of y, it will be 130.
4.
What will be the result of the following code? (Choose One)
public class Test {
public static void main(String[] args) {
System.out.println(0x12+015+15);
}
}
Correct Answer
A. Prints 46.
Explanation
The code snippet is performing addition of three numbers: 0x12, 015, and 15. In Java, a number starting with 0x is treated as a hexadecimal number, while a number starting with 0 is treated as an octal number. Therefore, 0x12 is equivalent to 18 in decimal, 015 is equivalent to 13 in decimal, and 15 is already in decimal. When these three numbers are added together, the result is 46.
5.
What will be the result of the following code? (Choose One)
public class Test {
public static void main(String[] args) {
System.out.println(- -2 - 6 * 12 / 8 - 1);
}
}
Correct Answer
B. Prints -8
Explanation
- -2 can be simplified as +2 because two consecutive negation signs cancel each other out.
Next, we have -6 * 12 / 8, which we solve according to the order of operations: multiplication and division before addition and subtraction. So, 6×126×12 equals 72, and then 72/872/8 equals 9.
Lastly, we have -1.
Putting it all together: +2−9−1+2−9−1
Adding and subtracting, we get: 2−9−1=−7−1=−82−9−1=−7−1=−8
So, the result of the given code will be -8 when executed.
6.
What will be the result of the following code? (Choose One)
public class Test {
public static void main(String[] args) {
int x = 2;
int y = ++x + x++ + + x;
System.out.println(y);
}
}
Correct Answer
C. Prints 10.
Explanation
The expression `++x` increments the value of `x` by 1 before it is used in the expression. `x++` increments the value of `x` by 1 after it is used in the expression. The `+` operator is used for addition.
In this code, the value of `x` is initially 2.
`++x` increments `x` to 3.
`x++` uses the current value of `x` (3) and then increments `x` to 4.
Finally, `+ x` adds the value of `x` (4) to the expression.
Therefore, the result is 3 + 3 + 4 = 10.
7.
What is the value of `result` after executing the code?
public class OperatorTest {
public static void main(String[] args) {
int a = 10, b = 20;
int result = a + b - a * b / a;
}
}
Correct Answer
A. 10
Explanation
The expression `a + b - a * b / a` involves several operators, and Java's operator precedence rules dictate how these operators are evaluated. The precedence and associativity rules lead to the following evaluation order:
1. `a * b` is calculated first because multiplication (*) has higher precedence than addition (+) and subtraction (-).
2. `a * b / a` is then calculated. Here, multiplication and division have the same precedence, but operations are performed from left to right due to their left associativity. The multiplication result is `200` (`10 * 20`), and then `200 / 10` equals `20`.
3. Finally, `a + b - result` from step 2 is calculated as `10 + 20 - 20`, which simplifies to `10`.
8.
What is the first line that will cause compile time error? (Choose One)
public class Test {
public static void main(String[] args) {
char a;
int b;
a = 'a';//1
b = a;//2
b--;//3
a = b;//4
}
}
Correct Answer
D. Line labeled 4
Explanation
Line labeled 4 will cause a compile time error because we are trying to assign an int value to a char variable. In Java, char variables can only store Unicode characters, not integer values. Therefore, the assignment of an int value to a char variable is not allowed and will result in a compile time error.
9.
What will be the result of compiling & running following code? (Choose One)
public class Test {
public static void main(String[] args) {
byte pizza = -128;
int wint = pizza;
System.out.println(wint);
}
}
Correct Answer
B. Prints -128.
Explanation
The code will compile and run without any errors. The variable "pizza" is assigned the value of -128 which is within the range of a byte. Then, the value of "pizza" is assigned to the variable "wint" of type int. Since int has a larger range than byte, the value -128 can be stored in the int variable without any loss of information. Finally, the value of "wint" is printed, which will be -128.
10.
Which of the following are legal assignments in Java? (Choose Three)
Correct Answer(s)
A. Short height = 12;
B. Long distance = 012;
C. Double trouble = 0x123456;
11.
Following Code will compile.
public class Test {
public static void main(String[] args) {
(10 < 12)?System.out.println("Java and C are sisters"):
System.out.println("Java and C are enemies");
}
}
Correct Answer
B. False
Explanation
The given code will not compile because the ternary operator is used without assigning the result to any variable or using it in any expression. In Java, the ternary operator is used to assign a value to a variable or to evaluate an expression based on a condition. In this case, the result of the ternary operator is not being used, which will result in a compilation error.
12.
Following Code will compile.
public class Test {
public static void main(String[] args) {
int leap = 2000;
System.out.println(leap%4==0?"Leap":"Not a Leap");
}
}
Correct Answer
A. True
Explanation
The code will compile because it does not contain any syntax errors. The main method is correctly written and the print statement is also correct. The code uses the ternary operator to check if the variable "leap" is divisible by 4 and prints "Leap" if it is, otherwise it prints "Not a Leap". Since there are no errors in the code, it will compile successfully.
13.
What will be the result of compiling and running following code? (Choose One)
public class Test {
public static void main(String[] args) {
System.out.println(7.0 % 5.2);
}
}
Correct Answer
A. Prints 1.7999999999999998
Explanation
The code will print "1.7999999999999998" because when the modulus operator (%) is used with floating-point numbers, it returns the remainder after division. In this case, 7.0 divided by 5.2 is approximately 1.3461538461538463, and the remainder is 1.7999999999999998.
14.
Which of the following are not legal operators in Java? (Choose Two)
Correct Answer(s)
B.
D. **
Explanation
The two operators that are not legal in Java are ">>>" and "**". The ">>>" operator is the unsigned right shift operator, which is not supported in Java. The "**" operator represents exponentiation, which is also not a valid operator in Java.
15.
Which of the following statements are true? (Choose Three)
Correct Answer(s)
B. Identifiers in Java are Case-Sensitive.
C. The arithmetic operators /, *, % have same precedence.
E. (+69) is a legal expression.
Explanation
The first statement is false because the modulus operator in Java can be applied to both int and floating-point quantities. The second statement is true because identifiers in Java are indeed case-sensitive. The third statement is false because the division (/) operator has higher precedence than the multiplication (*) and modulus (%) operators. The fourth statement is false because the range of the short data type in Java is -32,768 to +32,767 inclusive. The fifth statement is true because (+69) is a legal expression in Java.
16.
Given that int a may contain a negative value, which of the following are legal ways to double the value of a? (Choose Three)
Correct Answer(s)
A. A = a * 2;
B. A *=2;
D. A = a
Explanation
The given question asks for legal ways to double the value of a, which may contain a negative value. The first option, "a = a * 2;", is a valid way to double the value of a as it multiplies a by 2 and assigns the result back to a. The second option, "a *=2;", is also a legal way to double the value of a, as it is a shorthand notation for "a = a * 2;". The third option, "a =", is not a complete statement and does not double the value of a.
17.
Given the following, which of the given are valid assignments? (Choose Three)
byte kite = 10;
char alpha = 10;
short height = 110;
int p = 5;
Correct Answer(s)
B. P = kite * height;
C. Kite
E. Height += p;
Explanation
The given assignments are valid because they follow the rules of data type compatibility and assignment. In the first assignment, p is assigned the value of the product of kite and height, which is valid because the result can fit into an int data type. In the second assignment, kite is assigned the value of 10, which is valid because it fits into a byte data type. In the third assignment, height is incremented by the value of p, which is valid because the result can fit into a short data type.
18.
Which of the following is true about the following code? (Choose Two)
public class Test {
public static void main(String[] args) {
int a = 0 ;
int b = 0 ;
boolean tr = true;
boolean fa;
fa = (tr & 0 < (a += 1));
fa = (tr && 0 < (a += 2));
fa = (tr | 0 < (b += 1));
fa = (tr || 0 < (b += 2));
System.out.println(a + " " + b);
}
}
Correct Answer(s)
A. First digit printed is 3.
D. Second digit printed is 1.
Explanation
The code initializes two variables, a and b, to 0. It then declares a boolean variable tr and assigns it the value true. The code then declares a boolean variable fa.
In the first line, the expression (tr & 0 < (a += 1)) is evaluated. Since tr is true and 0 is less than a+1, the expression evaluates to true and assigns the value to fa. The value of a is incremented by 1.
In the second line, the expression (tr && 0 < (a += 2)) is evaluated. Again, tr is true and 0 is less than a+2, so the expression evaluates to true and assigns the value to fa. The value of a is incremented by 2.
In the third line, the expression (tr | 0 < (b += 1)) is evaluated. Since tr is true, the expression evaluates to true and assigns the value to fa. The value of b is incremented by 1.
In the fourth line, the expression (tr || 0 < (b += 2)) is evaluated. Since tr is true, the expression evaluates to true and assigns the value to fa. The value of b is not incremented because the short-circuit operator || is used.
Finally, the values of a and b are printed, resulting in "3 1" being printed.
19.
Following assignment is legal at compile-time.
int c = 3258.369;
Correct Answer
B. False
Explanation
The given assignment is not legal at compile-time because it is trying to assign a floating-point value (3258.369) to an integer variable (c). In order to assign a floating-point value to an integer variable, explicit typecasting is required.
20.
Following assignment is legal at compile-time.
byte might = 128;
Correct Answer
B. False
Explanation
The given assignment is not legal at compile-time because the variable "might" is declared as a byte, which can only hold values from -128 to 127. The value 128 exceeds the range of a byte, so it would result in a compile-time error.
21.
What will be the output of compiling and running following code? (Choose one)
public class Test {
public static void main(String[] args) {
long distance = 129L;
byte taken = (byte)distance;
System.out.println(taken);
}
}
Correct Answer
C. Prints -127.
Explanation
The code initializes a long variable 'distance' with the value 129L. It then tries to assign this value to a byte variable 'taken' by explicitly casting it. However, since the range of byte is from -128 to 127, the value 129 cannot be represented in a byte, resulting in an overflow. In Java, when an overflow occurs during a narrowing conversion, the excess bits are discarded, and the remaining bits are used to represent the value. In this case, the excess bits result in the value -127 being assigned to the 'taken' variable. Therefore, the code will print -127.
22.
What will be the output of compiling and running following code? (Choose one)
public class Test {
public static void main(String[] args) {
long distance = 129.36;
System.out.println(distance);
}
}
Correct Answer
B. Compile time Error.
Explanation
The code will result in a compile-time error because a long variable cannot store a decimal value. The variable "distance" is declared as a long, but it is assigned a decimal value of 129.36. To fix this error, the variable type should be changed to double or float to accommodate decimal values.
23.
What will be the output of compiling and running following code? (Choose one)
public class Test {
public static void main(String[] args) {
int x = 45;
String str = x < 40?"Money ": x > 50?"matters":"in life";
System.out.println(str);
}
}
Correct Answer
C. Prints "in life"
Explanation
The code uses the ternary operator to assign a value to the variable "str". The condition is x > 50. Since x is 45, the condition is false and the second part of the ternary operator is executed, which assigns the value "in life" to "str". Therefore, when the code is run, it will print "in life".
24.
What will be the output of compiling and running following code? (Choose one)
public class Test {
public static void main(String[] args) {
long distance = 42;
long name = 44;
System.out.print(" " + 7 + 2 + " ");
System.out.print("Hi" + distance + 5 + " ");
System.out.print(distance + name + "Bye");
}
}
Correct Answer
C. 72 Hi425 86Bye
Explanation
The code will output "72 Hi425 86Bye".
- The first print statement concatenates the numbers 7 and 2 with spaces, resulting in "7 2".
- The second print statement concatenates the string "Hi" with the value of the variable distance (42), the number 5, and a space, resulting in "Hi42 5".
- The third print statement adds the values of the variables distance (42) and name (44), resulting in 86, and concatenates it with the string "Bye", resulting in "86Bye".
25.
What will be the output of compiling and running following code? (Choose Two)
public class Test {
public static void main(String[] args) {
int x = 0,
y = 0;
if( ((10 < 12) || (++y < 15) | x++ < 5) x += 1;
if( (10 > 12) ^ false) ) x += 10;
if( !(x > 1) && ++y > 1 ) x += 200;
System.out.println(x + " " + y);
}
}
Correct Answer(s)
A. X is 2.
C. Y is 0.
Explanation
The code first checks the conditions inside the if statement. The first condition (10 < 12) is true, so the code proceeds to the next condition. The second condition (++y < 15) is also true, so the code proceeds to the next condition. The third condition (x++ < 5) is true, so the code increments x by 1. Therefore, x is 1 at this point.
The second if statement checks the condition (10 > 12) ^ false, which is false. Therefore, the code does not execute the code inside this if statement.
The third if statement checks the condition !(x > 1) && (++y > 1). Since x is 1, the condition !(x > 1) is false. Therefore, the code does not execute the code inside this if statement.
After the if statements, x is still 1 and y is 1.
Therefore, the output of the code is "1 1".
26.
What will be the result of the following code? (Choose One)
public class MultiCast {
public static void main(String args[]) {
System.out.println((int)(char)(byte) -1);
}
}
Correct Answer
C. Prints 65535.
Explanation
The code first casts -1 to a byte, then casts the byte to a char, and finally casts the char to an int. When a negative value is cast to a byte, it is sign-extended, meaning the resulting byte will have all 1's in its binary representation. When this byte is cast to a char, it is zero-extended, meaning the resulting char will have all 0's in its binary representation except for the most significant bit. Finally, when the char is cast to an int, it is also zero-extended. Therefore, the resulting int will have all 0's in its binary representation except for the most significant bit, which will be 1. This binary representation corresponds to the decimal value 65535, so the code will print 65535.
27.
What will be the result of the following code? (Choose One)
public class MultiCast {
public static void main(String args[]) {
System.out.println(-2%4);
}
}
Correct Answer
A. Prints -2.
Explanation
The code is calculating the remainder of -2 divided by 4 using the modulus operator (%). The result of this calculation is -2, so the program will print -2.
28.
What will be the result of the following code? (Choose One)
public class Laughable {
public static void main(String args[]) {
System.out.print(“H” + “a”);
System.out.print(‘H’ + ‘a’);
System.out.print(“” + ‘H’ + ‘a’);
}
}
Correct Answer
B. Prints Ha169Ha.
Explanation
The code is attempting to concatenate different characters and strings to print a specific output. In the first line, "H" and "a" are concatenated using the "+" operator, resulting in "Ha". In the second line, a single quote character 'H' is concatenated with the character 'a' using the "+" operator. However, this will result in a compilation error because a single quote character can only be used to represent a single character, not a string. In the third line, an empty string is concatenated with the character 'H' and the character 'a', resulting in "Ha". Finally, the code prints "Ha169Ha" as the output.
29.
What will be the result of the following code? (Choose One)
public class Bitwise {
public static void main(String args[]) {
int i = 32, j = 65, k, l, m, n, o, p;
k = i | 35;
l = ~k;
m = i & j;
n = j ^ 32;
o = j << 2;
p = i >> 5;
System.out.println(“k = “ + k + “m = ” + m + “n = “ + n);
System.out.println(“ o = ” + o + “ p = “ + p);
}
}
Correct Answer
A. Prints k = 35 m = 0 n = 97 o = 260 p = 1
Explanation
The code first assigns the value 35 to variable k by performing a bitwise OR operation between i and 35. Then, it assigns the bitwise complement of k to variable l. The bitwise AND operation between i and j is assigned to variable m. The bitwise XOR operation between j and 32 is assigned to variable n. The value of j is shifted left by 2 bits and assigned to variable o. Finally, the value of i is shifted right by 5 bits and assigned to variable p. The code then prints the values of k, m, n, o, and p. The expected output matches the given answer.
30.
What will be the result of the following code? (Choose One)
public class Bitwise {
public static void main(String args[]) {
System.out.println(1<<31>>1 == 1<<31>>>1?"Both are same":"Not Same");
}
}
Correct Answer
B. Prints Not Same.
Explanation
The code is using bitwise operators to shift the binary representation of the number 1. The expression "11" shifts the binary representation of 1 to the left by 31 bits, and then shifts it back to the right by 1 bit. The expression "1>1" also shifts the binary representation of 1 to the left by 31 bits, but uses the unsigned right shift operator ">>>" instead of ">>".
The unsigned right shift operator fills the empty bits on the left with zeros, while the signed right shift operator fills the empty bits on the left with the sign bit (in this case, 1).
Since the signed right shift operator is used in the first expression and the unsigned right shift operator is used in the second expression, the two expressions will have different results. Therefore, the code will print "Not Same".