Assignments & Operator Precedence In Java

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Kalloorkar_bvk
K
Kalloorkar_bvk
Community Contributor
Quizzes Created: 2 | Total Attempts: 1,086
| Attempts: 739 | Questions: 30
Please wait...
Question 1 / 30
0 %
0/100
Score 0/100
1. 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");        } }

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.

Submit
Please wait...
About This Quiz
Assignments & Operator Precedence In Java - Quiz

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

2. 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);        } }

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.

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

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.

Submit
4. 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);        } }

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.

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

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;

Submit
6. 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);        } }

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.

Submit
7. 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);        } }

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.

Submit
8. 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);        } }

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.

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

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.

Submit
10. 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        } }

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.

Submit
11. 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);        } }

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

Submit
12. 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);        } }

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.

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

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.

Submit
14. Which of the following are legal assignments in Java? (Choose Three)

Explanation

not-available-via-ai

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

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

Submit
16. 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);        } }

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.

Submit
17. Given that int a may contain a negative value, which of the following are legal ways to double the value of a? (Choose Three)

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.

Submit
18. 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');        } }

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.

Submit
19. 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);        } }

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.

Submit
20. 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;     } }

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

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

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.

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

Explanation

The code is using bitwise operators to shift the binary representation of the number 1. The expression "1>1" 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".

Submit
23. Which of the following are not legal operators in Java? (Choose Two)

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.

Submit
24. Which of the following statements are true? (Choose Three)

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.

Submit
25. 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);        } }

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
In the second line, the expression (tr && 0
In the third line, the expression (tr | 0
In the fourth line, the expression (tr || 0
Finally, the values of a and b are printed, resulting in "3 1" being printed.

Submit
26. 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);        } }

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.

Submit
27. 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);        } }

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.

Submit
28. 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);        } }

Explanation

The code first checks the conditions inside the if statement. The first condition (10
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".

Submit
29. 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);        } }

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.

Submit
30. Given the following, which of the given are valid assignments? (Choose Three) byte kite = 10; char alpha = 10; short height = 110; int p = 5;

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.

Submit
View My Results

Quiz Review Timeline (Updated): May 15, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • May 15, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 28, 2012
    Quiz Created by
    Kalloorkar_bvk
Cancel
  • All
    All (30)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Following Code will compile....
What will be the result of the following code? (Choose One)...
Following assignment is legal at compile-time. int c = 3258.369;
What will be the result of compiling & running following code?...
Following code will compile....
What will be the result of the following code? (Choose One) ...
What will be the output of compiling and running following code?...
What will be the result of the following code? (Choose One)...
Following assignment is legal at compile-time. byte might = 128;
What is the first line that will cause compile time error? (Choose...
What will be the output of compiling and running following code?...
What will be the result of the following code? (Choose One)...
What is easiest way to assign the value to an integer for the given...
Which of the following are legal assignments in Java? (Choose Three)
What will be the output of compiling and running following code?...
What will be the result of the following code? (Choose One)...
Given that int a may contain a negative value, which of the following...
What will be the result of the following code? (Choose One)...
What will be the result of compiling and running following code?...
What is the value of `result` after executing the code? ...
Following Code will compile....
What will be the result of the following code? (Choose One)...
Which of the following are not legal operators in Java? (Choose Two)
Which of the following statements are true? (Choose Three)
Which of the following is true about the following code? (Choose...
What will be the output of compiling and running following code?...
What will be the result of the following code? (Choose One)...
What will be the output of compiling and running following code?...
What will be the result of the following code? (Choose One)...
Given the following, which of the given are valid assignments? (Choose...
Alert!

Advertisement