Java Questions & Answers – Assignment Operators and Operator Precedence

This section of our 1000+ Java MCQs focuses on assignment operators and operator precedence in Java Programming Language.

1. Which of these have highest precedence?
a) ()
b) ++
c) *
d) >>
View Answer

Answer: a
Explanation: Order of precedence is (highest to lowest) a -> b -> c -> d.

2. What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
a) Integer
b) Floating – point numbers
c) Boolean
d) None of the mentioned
View Answer

Answer:c
Explanation: The controlling condition of ternary operator must evaluate to boolean.

3. What is the value stored in x in following lines of code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
a) 0
b) 1
c) 9
d) 8
View Answer

Answer: d
Explanation: None.

4. What is the order of precedence (highest to lowest) of following operators?
1. &
2. ^
3. ?:
a) 1 -> 2 -> 3
b) 2 -> 1 -> 3
c) 3 -> 2 -> 1
d) 2 -> 3 -> 1
View Answer

Answer: a
Explanation: None.

5. Which of these statements are incorrect?
a) Equal to operator has least precedence.
b) Brackets () have highest precedence.
c) Division operator, /, has higher precedence than multiplication operator.
d) Addition operator, +, and subtraction operator have equal precedence.
View Answer

Answer: c
Explanation: Division operator, /, has equal precedence as of multiplication operator. In expression involving multiplication and division evaluation of expression will begin from right side when no brackets are used.

6. What is the output of this program?

  1.     class operators {
  2.         public static void main(String args[])
  3.         {
  4.             int var1 = 5; 
  5.             int var2 = 6;
  6.             int var3;
  7.             var3 = ++ var2 * var1 / var2 + var2;
  8.             System.out.print(var3);
  9.         } 
  10.     }

a) 10
b) 11
c) 12
d) 56
View Answer

Answer:c
Explanation: Operator ++ has the highest precedence than / , * and +. var2 is incremented to 7 and then used in expression, var3 = 7 * 5 / 7 + 7, gives 12.
output:
$ javac operators.java
$ java operators
12

7. What is the output of this program?

  1.     class operators {
  2.         public static void main(String args[]) 
  3.         {    
  4.              int x = 8;
  5.              System.out.println(++x * 3 + " " + x);
  6.         } 
  7.     }

a) 24 8
b) 24 9
c) 27 8
d) 27 9
View Answer

Answer: d
Explanation: Operator ++ has higher precedence than multiplication operator, *, x is incremented to 9 than multiplied with 3 giving 27.
output:
$ javac operators.java
$ java operators
27 9

8. What is the output of this program?

  1.     class ternary_operator {
  2.         public static void main(String args[]) 
  3.         {        
  4.              int x = 3;
  5.              int y = ~ x;
  6.              int z;
  7.              z = x > y ? x : y;
  8.              System.out.print(z);
  9.         } 
  10.     }

a) 0
b) 1
c) 3
d) -4
View Answer

Answer:c
Explanation: None.
output:
$ javac ternary_operator.java
$ java ternary_operator
3

9. Which of these lines of code will give better performance?
1. a | 4 + c >> b & 7;
2. (a | ((( 4 * c ) >> b ) & 7 ))
a) 1 will give better performance as it has no parentheses.
b) 2 will give better performance as it has parentheses.
c) Both 1 & 2 will give equal performance.
d) Dependent on the computer system.
View Answer

Answer: c
Explanation: Parentheses do not degrade the performance of the program. Adding parentheses to reduce ambiguity does not negatively affect your system.

10. What is the output of this program?

  1.     class Output {
  2.         public static void main(String args[]) 
  3.         {    
  4.              int x , y = 1;
  5.              x = 10;
  6.              if (x != 10 && x / 0 == 0)
  7.                  System.out.println(y);
  8.              else
  9.                  System.out.println(++y);
  10.         } 
  11.     }

a) 1
b) 2
c) Runtime error owing to division by zero in if condition.
d) Unpredictable behavior of program.
View Answer

Answer: b
Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error.
output:
$ javac Output.java
$ java Output
2

Sanfoundry Global Education & Learning Series – Java Programming Language.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry