# Assignments & Operator Precedence In Java

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
| By Kalloorkar_bvk
K
Kalloorkar_bvk
Community Contributor
Quizzes Created: 2 | Total Attempts: 939
Questions: 30 | Attempts: 601

Settings

Tests rules for assigning values to variables and operator precedence in Java

• 1.

### Following code will compile. public class TestOnOprs {        public static void main(String[] args) {              float f = 36.25;              System.out.println(f);        } }

• A.

True

• B.

False

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;

Rate this question:

• 2.

### What is easiest way to assign the value to an integer for the given declaration? (Choose One) char x = 'Y';

• A.

Int i = Character.getNumericValue(c);

• B.

Int i = (int)c;

• C.

Int i = c;

• D.

Int i = Integer.parseInt(c.toString());

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.

Rate this question:

• 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);        } }

• A.

Compilation error due to 3 assignments in a row

• B.

Prints 130

• C.

Prints 100

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.

Rate this question:

• 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);        } }

• A.

Prints 46.

• B.

Prints 42

• C.

Compilation error as 0x & 0 are funny symbols for Java

• D.

Compilation error because these constants must be stored in variables first

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.

Rate this question:

• 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);        } }

• A.

Prints -7.

• B.

Prints -8

• C.

Compilation error as expressions can't be given directly into System.out.println

• D.

Compilation error because these constants must be stored in variables first

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.

Rate this question:

• 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);        } }

• A.

Prints 9.

• B.

Prints 11

• C.

Prints 10.

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.

Rate this question:

• 7.

### What will be the result of the following code? (Choose One) public class Test {        public static void main(String[] args) {            int x = 2;            int x = x++;            System.out.println(x);        } }

• A.

Prints 2.

• B.

Prints 3

• C.

Prints 4

A. Prints 2.
Explanation
The code initializes the variable x to 2. Then, it assigns the value of x to x++, which is a post-increment operation. In a post-increment operation, the value of the variable is first used and then incremented. So, the value of x remains 2 when it is printed. Therefore, the code will print 2.

Rate this question:

• 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        } }

• A.

Line labeled 1

• B.

Line labeled 2

• C.

Line labeled 3

• D.

Line labeled 4

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.

Rate this question:

• 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);        } }

• A.

Compile time error byte can't be assigned to int.

• B.

Prints -128.

• C.

Prints 127.

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.

Rate this question:

• 10.

### Which of the following are legal assignments in Java? (Choose Three)

• A.

Short height = 12;

• B.

Long distance = 012;

• C.

Double trouble = 0x123456;

• D.

Int yesno = (int) true;

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");        } }

• A.

True

• B.

False

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.

Rate this question:

• 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");        } }

• A.

True

• B.

False

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.

Rate this question:

• 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);        } }

• A.

Prints 1.7999999999999998

• B.

This is not legal as in C.

• C.

Prints 1.8

• D.

Throws ArithmeticException at runtime.

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.

Rate this question:

• 14.

### Which of the following are not legal operators in Java? (Choose Two)

• A.

%

• B.

• C.

>>>

• D.

**

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.

Rate this question:

• 15.

### Which of the following statements are true? (Choose Three)

• A.

Modulus operator in Java are applicable to int quantities only.

• B.

Identifiers in Java are Case-Sensitive.

• C.

The arithmetic operators /, *, % have same precedence.

• D.

Range of the data type short is -128 to +127 inclusive.

• E.

(+69) is a legal expression.

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.

Rate this question:

• 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)

• A.

A = a * 2;

• B.

A *=2;

• C.

A

• D.

A = a

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.

Rate this question:

• 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;

• A.

Height = kite * 5;

• B.

P = kite * height;

• C.

Kite

• D.

Alpha = alpha + kite;

• E.

Height += p;

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.

Rate this question:

• 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);        } }

• A.

First digit printed is 3.

• B.

First digit printed is 2

• C.

Second digit printed is 3.

• D.

Second digit printed is 1.

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.

Rate this question:

• 19.

### Following assignment is legal at compile-time. int c = 3258.369;

• A.

True

• B.

False

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.

Rate this question:

• 20.

### Following assignment is legal at compile-time. byte might = 128;

• A.

True

• B.

False

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.

Rate this question:

• 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);        } }

• A.

Prints -126.

• B.

Compile time Error.

• C.

Prints -127.

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.

Rate this question:

• 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);        } }

• A.

Prints 129.

• B.

Compile time Error.

• C.

Prints 130

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.

Rate this question:

• 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);        } }

• A.

Null.

• B.

Prints "Money"

• C.

Prints "in life"

• D.

Prints "matters"

• E.

Compilation fails.

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

Rate this question:

• 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");        } }

• A.

9 Hi47 87Bye

• B.

72 Hi47 4244Bye

• C.

72 Hi425 86Bye

• D.

Compilation fails.

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

Rate this question:

• 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);        } }

• A.

X is 2.

• B.

X is 211.

• C.

Y is 0.

• D.

Y is 2.

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

Rate this question:

• 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);        } }

• A.

Prints -1.

• B.

Prints 0.

• C.

Prints 65535.

• D.

Compilation Error.

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.

Rate this question:

• 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);        } }

• A.

Prints -2.

• B.

Prints 2.

• C.

Prints 0.

• D.

Compilation Error.

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.

Rate this question:

• 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’);        } }

• A.

Prints Ha Ha Ha.

• B.

Prints Ha169Ha.

• C.

Compilation Error at second System.out.print.

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.

Rate this question:

• 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);        } }

• A.

Prints k = 35 m = 0 n = 97 o = 260 p = 1

• B.

Prints k = 20 m = -9 n = 90 o = 262 p = 0

• C.

Compilation Error.

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.

Rate this question:

• 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");        } }

• A.

Prints Both are Same.

• B.

Prints Not Same.

• C.

Compilation Error.

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

Rate this question:

Related Topics