• Java MCQ Topics
  • Operators & Assignments
  • Regular Expressions
  • Garbage Collection
  • Exception Handling
  • File Handling
  • Flow Control
  • Inheritance
  • Abstract Class
  • Command Line Arguments
  • Java Programming Reference
  • Learn Java Programming
  • Java Programming Books
  • Java Interview Questions
  • Java Coding Questions
  • Java Programming Quiz
  • Other Reference
  • Quantitative Aptitude
  • Learn Firebase
  • Android Books

Java MCQ Questions - Operators & Assignments

This section focuses on the "operators and assignments" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.

A. Characters B. Boolean C. Numeric D. Both Numeric & Characters

Explanation: The operand of arithmetic operators can be any of numeric or character type, But not boolean.

A. Both Integers and floating - point numbers B. Integers C. Floating - point numbers D. None of the mentioned

Explanation: Modulus operator can be applied to both integers and floating point numbers..

A. 1 B. 2 C. 3 D. 4

Explanation: Decrement operator, −−, decreases the value of variable by 1.

A. Assignment operators can be used only with numeric and character data type B. Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms C. Assignment operators run faster than their equivalent long forms D. None of the mentioned

Explanation: None of the mentioned.

A. TRUE B. FALSE C. Can be true or false D. can not say

Explanation: Both data types have different memory representation that is why 8-byte integral data type can be stored to 4-byte floating point data type.

A. 14 B. 12 C. 13 D. 10

Explanation: Output of the expression is 13.

A. ( ) B. { } C. Both A & B D. None of these

Explanation: Parentheses(), Array subscript{} and Member selection- have the same precedence.

A. Compare two boolean values B. Compare two numeric values C. Combine two boolean values D. Combine two numeric values

Explanation: Logical AND(&&) and Logical OR(||) combine two boolean values.

A. ((x 1)) && (x B. ((x 1)) || (x C. (1 > x > 100) || (x D. 1

Explanation: No explanation.

A. \\ B. \v C. \a D. \t

A. 12 15 B. 15 15 C. 3 7 5 3 7 5 D. 3 4 5 3 7 5

Explanation: The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object. So Output: 3+7+5+"" ""3+7+5 Output: 15 15 Because Numeric values will be added

A. true true B. true false C. false true D. false false

Explanation: The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method.

A. s st B. sst st C. st s st D. sst s st

Explanation: When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of ""s""). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of ""sst""). When the program returns to start(), another String object is created, referred to by s2 and with a value of ""st"".

A. (int)Math.min(d); B. (int)Math.abs(d); C. (int)Math.max(d); D. (int)Math.floor(d);

Explanation: The casting to an int is a smokescreen.

A. Compiler Error: Operator >> cannot be applied to negative numbers B. -2 2 C. 2 D. 2 2

Explanation: No explantion.

A. 1, 2 & 3 B. 1 & 4 C. 1, 2, 3 & 4 D. 3 & 2

Explanation: Operator ++ increases value of variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also x =+ 1 will set the value of x to 1.

A. 0 1 B. 1 1 C. 1.5 1 D. 1.5 1.0

Explanation: No Explanation.

A. s B. t C. h D. Compilation fails

Explanation: This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the ""t"" value is assigned to sup.

A. 7 B. 0 C. 14 D. 8

Explanation: The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.

A. 44 B. 56 C. 48 D. 40

Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.

A. Integer B. Boolean C. Characters D. Double

Explanation: None.

A. Integers B. Floating - point numbers C. Boolean D. None of the mentioned

Explanation: All relational operators return a boolean value ie. true and false.

A. && B. == C. ?: D. +=

Explanation: Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values.

A. ! B. | C. & D. &&

Explanation: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be determined by left operand alone.

A. true and false are numeric values 1 and 0 B. true and false are numeric values 0 and 1 C. true is any non zero value and false is 0 D. true and false are non numeric values

Explanation: True and false are keywords, they are non numeric values which do not relate to zero or non zero numbers. true and false are boolean values.

A. 1 B. 0 C. TRUE D. FALSE

Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned. output: $ javac Relational_operator.java $ java Relational_operator false

A. 0 B. 1 C. 3 D. -4

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

A. 1 B. 2 C. Runtime error owing to division by zero in if condition D. Unpredictable behavior of program

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

A. 0 B. 1 C. FALSE D. TRUE

Explanation: None. output: $ javac Output.java $ java Output false

A. () B. ++ C. * D. >>

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

A. Integer B. Floating - point numbers C. Boolean D. None of the mentioned

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

A. 0 B. 1 C. 9 D. 8

A. 1 -> 2 -> 3 B. 2 -> 1 -> 3 C. 3 -> 2 -> 1 D. 2 -> 3 -> 1

A. 10 B. 11 C. 12 D. 56

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

A. 24 8 B. 24 9 C. 27 8 D. 27 9

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

Also check :

  • Bootstrap Courses
  • Best Sql Tutorial

* You must be logged in to add comment.

ITDeveloper

Redefining your limits, welcome, guest.

Multiple Choice Questions

Share with a Friend

Java - multiple choice questions (mcq) - introduction to java - set 1, fixed width.

This is a dashboard layout for Bootstrap 4. This is an example of the Modal component which you can use to show content. Any content can be placed inside the modal and it can use the Bootstrap grid classes.

Grab the code at Codeply

Library homepage

  • school Campus Bookshelves
  • menu_book Bookshelves
  • perm_media Learning Objects
  • login Login
  • how_to_reg Request Instructor Account
  • hub Instructor Commons

Margin Size

  • Download Page (PDF)
  • Download Full Book (PDF)
  • Periodic Table
  • Physics Constants
  • Scientific Calculator
  • Reference & Cite
  • Tools expand_more
  • Readability

selected template will load here

This action is not available.

Engineering LibreTexts

4.5: Assignment Operator

  • Last updated
  • Save as PDF
  • Page ID 10258

  • Kenneth Leroy Busbee
  • Houston Community College via OpenStax CNX

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

\( \newcommand{\Span}{\mathrm{span}}\)

\( \newcommand{\id}{\mathrm{id}}\)

\( \newcommand{\kernel}{\mathrm{null}\,}\)

\( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\)

\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\)

\( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

\( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vectorC}[1]{\textbf{#1}} \)

\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated with the concept of moving a value into the storage location (again usually a variable). Within C++ programming language the symbol used is the equal symbol. But bite your tongue, when you see the = symbol you need to start thinking: assignment. The assignment operator has two operands. The one to the left of the operator is usually an identifier name for a variable. The one to the right of the operator is a value.

The value 21 is moved to the memory location for the variable named: age. Another way to say it: age is assigned the value 21.

The item to the right of the assignment operator is an expression. The expression will be evaluated and the answer is 14. The value 14 would assigned to the variable named: total_cousins.

The expression to the right of the assignment operator contains some identifier names. The program would fetch the values stored in those variables; add them together and get a value of 44; then assign the 44 to the total_students variable.

Definitions

assignment operators are more efficiently implemented mcq

  • Table of Contents
  • Course Home
  • Assignments
  • Peer Instruction (Instructor)
  • Peer Instruction (Student)
  • Change Course
  • Instructor's Page
  • Progress Page
  • Edit Profile
  • Change Password
  • Scratch ActiveCode
  • Scratch Activecode
  • Instructors Guide
  • About Runestone
  • Report A Problem
  • 1.1 Preface
  • 1.2 Why Programming? Why Java?
  • 1.3 Variables and Data Types
  • 1.4 Expressions and Assignment Statements
  • 1.5 Compound Assignment Operators
  • 1.6 Casting and Ranges of Variables
  • 1.7 Java Development Environments (optional)
  • 1.8 Unit 1 Summary
  • 1.9 Unit 1 Mixed Up Code Practice
  • 1.10 Unit 1 Coding Practice
  • 1.11 Multiple Choice Exercises
  • 1.12 Lesson Workspace
  • 1.3. Variables and Data Types" data-toggle="tooltip">
  • 1.5. Compound Assignment Operators' data-toggle="tooltip" >

1.4. Expressions and Assignment Statements ¶

In this lesson, you will learn about assignment statements and expressions that contain math operators and variables.

1.4.1. Assignment Statements ¶

Remember that a variable holds a value that can change or vary. Assignment statements initialize or change the value stored in a variable using the assignment operator = . An assignment statement always has a single variable on the left hand side of the = sign. The value of the expression on the right hand side of the = sign (which can contain math operators and other variables) is copied into the memory location of the variable on the left hand side.

Assignment statement

Figure 1: Assignment Statement (variable = expression) ¶

Instead of saying equals for the = operator in an assignment statement, say “gets” or “is assigned” to remember that the variable on the left hand side gets or is assigned the value on the right. In the figure above, score is assigned the value of 10 times points (which is another variable) plus 5.

The following video by Dr. Colleen Lewis shows how variables can change values in memory using assignment statements.

As we saw in the video, we can set one variable to a copy of the value of another variable like y = x;. This won’t change the value of the variable that you are copying from.

coding exercise

Click on the Show CodeLens button to step through the code and see how the values of the variables change.

The program is supposed to figure out the total money value given the number of dimes, quarters and nickels. There is an error in the calculation of the total. Fix the error to compute the correct amount.

Calculate and print the total pay given the weekly salary and the number of weeks worked. Use string concatenation with the totalPay variable to produce the output Total Pay = $3000 . Don’t hardcode the number 3000 in your print statement.

exercise

Assume you have a package with a given height 3 inches and width 5 inches. If the package is rotated 90 degrees, you should swap the values for the height and width. The code below makes an attempt to swap the values stored in two variables h and w, which represent height and width. Variable h should end up with w’s initial value of 5 and w should get h’s initial value of 3. Unfortunately this code has an error and does not work. Use the CodeLens to step through the code to understand why it fails to swap the values in h and w.

1-4-7: Explain in your own words why the ErrorSwap program code does not swap the values stored in h and w.

Swapping two variables requires a third variable. Before assigning h = w , you need to store the original value of h in the temporary variable. In the mixed up programs below, drag the blocks to the right to put them in the right order.

The following has the correct code that uses a third variable named “temp” to swap the values in h and w.

The code is mixed up and contains one extra block which is not needed in a correct solution. Drag the needed blocks from the left into the correct order on the right, then check your solution. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks.

After three incorrect attempts you will be able to use the Help Me button to make the problem easier.

Fix the code below to perform a correct swap of h and w. You need to add a new variable named temp to use for the swap.

1.4.2. Incrementing the value of a variable ¶

If you use a variable to keep score you would probably increment it (add one to the current value) whenever score should go up. You can do this by setting the variable to the current value of the variable plus one (score = score + 1) as shown below. The formula looks a little crazy in math class, but it makes sense in coding because the variable on the left is set to the value of the arithmetic expression on the right. So, the score variable is set to the previous value of score + 1.

Click on the Show CodeLens button to step through the code and see how the score value changes.

1-4-11: What is the value of b after the following code executes?

  • It sets the value for the variable on the left to the value from evaluating the right side. What is 5 * 2?
  • Correct. 5 * 2 is 10.

1-4-12: What are the values of x, y, and z after the following code executes?

  • x = 0, y = 1, z = 2
  • These are the initial values in the variable, but the values are changed.
  • x = 1, y = 2, z = 3
  • x changes to y's initial value, y's value is doubled, and z is set to 3
  • x = 2, y = 2, z = 3
  • Remember that the equal sign doesn't mean that the two sides are equal. It sets the value for the variable on the left to the value from evaluating the right side.
  • x = 1, y = 0, z = 3

1.4.3. Operators ¶

Java uses the standard mathematical operators for addition ( + ), subtraction ( - ), multiplication ( * ), and division ( / ). Arithmetic expressions can be of type int or double. An arithmetic operation that uses two int values will evaluate to an int value. An arithmetic operation that uses at least one double value will evaluate to a double value. (You may have noticed that + was also used to put text together in the input program above – more on this when we talk about strings.)

Java uses the operator == to test if the value on the left is equal to the value on the right and != to test if two items are not equal. Don’t get one equal sign = confused with two equal signs == ! They mean different things in Java. One equal sign is used to assign a value to a variable. Two equal signs are used to test a variable to see if it is a certain value and that returns true or false as you’ll see below. Use == and != only with int values and not doubles because double values are an approximation and 3.3333 will not equal 3.3334 even though they are very close.

Run the code below to see all the operators in action. Do all of those operators do what you expected? What about 2 / 3 ? Isn’t surprising that it prints 0 ? See the note below.

When Java sees you doing integer division (or any operation with integers) it assumes you want an integer result so it throws away anything after the decimal point in the answer, essentially rounding down the answer to a whole number. If you need a double answer, you should make at least one of the values in the expression a double like 2.0.

With division, another thing to watch out for is dividing by 0. An attempt to divide an integer by zero will result in an ArithmeticException error message. Try it in one of the active code windows above.

Operators can be used to create compound expressions with more than one operator. You can either use a literal value which is a fixed value like 2, or variables in them. When compound expressions are evaluated, operator precedence rules are used, so that *, /, and % are done before + and -. However, anything in parentheses is done first. It doesn’t hurt to put in extra parentheses if you are unsure as to what will be done first.

In the example below, try to guess what it will print out and then run it to see if you are right. Remember to consider operator precedence .

1-4-15: Consider the following code segment. Be careful about integer division.

What is printed when the code segment is executed?

  • 0.666666666666667
  • Don't forget that division and multiplication will be done first due to operator precedence.
  • Yes, this is equivalent to (5 + ((a/b)*c) - 1).
  • Don't forget that division and multiplication will be done first due to operator precedence, and that an int/int gives an int result where it is rounded down to the nearest int.

1-4-16: Consider the following code segment.

What is the value of the expression?

  • Dividing an integer by an integer results in an integer
  • Correct. Dividing an integer by an integer results in an integer
  • The value 5.5 will be rounded down to 5

1-4-17: Consider the following code segment.

  • Correct. Dividing a double by an integer results in a double
  • Dividing a double by an integer results in a double

1-4-18: Consider the following code segment.

  • Correct. Dividing an integer by an double results in a double
  • Dividing an integer by an double results in a double

1.4.4. The Modulo Operator ¶

The percent sign operator ( % ) is the mod (modulo) or remainder operator. The mod operator ( x % y ) returns the remainder after you divide x (first number) by y (second number) so 5 % 2 will return 1 since 2 goes into 5 two times with a remainder of 1. Remember long division when you had to specify how many times one number went into another evenly and the remainder? That remainder is what is returned by the modulo operator.

../_images/mod-py.png

Figure 2: Long division showing the whole number result and the remainder ¶

In the example below, try to guess what it will print out and then run it to see if you are right.

The result of x % y when x is smaller than y is always x . The value y can’t go into x at all (goes in 0 times), since x is smaller than y , so the result is just x . So if you see 2 % 3 the result is 2 .

1-4-21: What is the result of 158 % 10?

  • This would be the result of 158 divided by 10. modulo gives you the remainder.
  • modulo gives you the remainder after the division.
  • When you divide 158 by 10 you get a remainder of 8.

1-4-22: What is the result of 3 % 8?

  • 8 goes into 3 no times so the remainder is 3. The remainder of a smaller number divided by a larger number is always the smaller number!
  • This would be the remainder if the question was 8 % 3 but here we are asking for the reminder after we divide 3 by 8.
  • What is the remainder after you divide 3 by 8?

1.4.5. FlowCharting ¶

Assume you have 16 pieces of pizza and 5 people. If everyone gets the same number of slices, how many slices does each person get? Are there any leftover pieces?

In industry, a flowchart is used to describe a process through symbols and text. A flowchart usually does not show variable declarations, but it can show assignment statements (drawn as rectangle) and output statements (drawn as rhomboid).

The flowchart in figure 3 shows a process to compute the fair distribution of pizza slices among a number of people. The process relies on integer division to determine slices per person, and the mod operator to determine remaining slices.

Flow Chart

Figure 3: Example Flow Chart ¶

A flowchart shows pseudo-code, which is like Java but not exactly the same. Syntactic details like semi-colons are omitted, and input and output is described in abstract terms.

Complete the program based on the process shown in the Figure 3 flowchart. Note the first line of code declares all 4 variables as type int. Add assignment statements and print statements to compute and print the slices per person and leftover slices. Use System.out.println for output.

1.4.6. Storing User Input in Variables ¶

Variables are a powerful abstraction in programming because the same algorithm can be used with different input values saved in variables.

Program input and output

Figure 4: Program input and output ¶

A Java program can ask the user to type in one or more values. The Java class Scanner is used to read from the keyboard input stream, which is referenced by System.in . Normally the keyboard input is typed into a console window, but since this is running in a browser you will type in a small textbox window displayed below the code. The code below shows an example of prompting the user to enter a name and then printing a greeting. The code String name = scan.nextLine() gets the string value you enter as program input and then stores the value in a variable.

Run the program a few times, typing in a different name. The code works for any name: behold, the power of variables!

Run this program to read in a name from the input stream. You can type a different name in the input window shown below the code.

Try stepping through the code with the CodeLens tool to see how the name variable is assigned to the value read by the scanner. You will have to click “Hide CodeLens” and then “Show in CodeLens” to enter a different name for input.

The Scanner class has several useful methods for reading user input. A token is a sequence of characters separated by white space.

Run this program to read in an integer from the input stream. You can type a different integer value in the input window shown below the code.

A rhomboid (slanted rectangle) is used in a flowchart to depict data flowing into and out of a program. The previous flowchart in Figure 3 used a rhomboid to indicate program output. A rhomboid is also used to denote reading a value from the input stream.

Flow Chart

Figure 5: Flow Chart Reading User Input ¶

Figure 5 contains an updated version of the pizza calculator process. The first two steps have been altered to initialize the pizzaSlices and numPeople variables by reading two values from the input stream. In Java this will be done using a Scanner object and reading from System.in.

Complete the program based on the process shown in the Figure 5 flowchart. The program should scan two integer values to initialize pizzaSlices and numPeople. Run the program a few times to experiment with different values for input. What happens if you enter 0 for the number of people? The program will bomb due to division by zero! We will see how to prevent this in a later lesson.

The program below reads two integer values from the input stream and attempts to print the sum. Unfortunately there is a problem with the last line of code that prints the sum.

Run the program and look at the result. When the input is 5 and 7 , the output is Sum is 57 . Both of the + operators in the print statement are performing string concatenation. While the first + operator should perform string concatenation, the second + operator should perform addition. You can force the second + operator to perform addition by putting the arithmetic expression in parentheses ( num1 + num2 ) .

More information on using the Scanner class can be found here https://www.w3schools.com/java/java_user_input.asp

1.4.7. Programming Challenge : Dog Years ¶

In this programming challenge, you will calculate your age, and your pet’s age from your birthdates, and your pet’s age in dog years. In the code below, type in the current year, the year you were born, the year your dog or cat was born (if you don’t have one, make one up!) in the variables below. Then write formulas in assignment statements to calculate how old you are, how old your dog or cat is, and how old they are in dog years which is 7 times a human year. Finally, print it all out.

Calculate your age and your pet’s age from the birthdates, and then your pet’s age in dog years. If you want an extra challenge, try reading the values using a Scanner.

1.4.8. Summary ¶

Arithmetic expressions include expressions of type int and double.

The arithmetic operators consist of +, -, * , /, and % (modulo for the remainder in division).

An arithmetic operation that uses two int values will evaluate to an int value. With integer division, any decimal part in the result will be thrown away, essentially rounding down the answer to a whole number.

An arithmetic operation that uses at least one double value will evaluate to a double value.

Operators can be used to construct compound expressions.

During evaluation, operands are associated with operators according to operator precedence to determine how they are grouped. (*, /, % have precedence over + and -, unless parentheses are used to group those.)

An attempt to divide an integer by zero will result in an ArithmeticException to occur.

The assignment operator (=) allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.

During execution, expressions are evaluated to produce a single value.

The value of an expression has a type based on the evaluation of the expression.

TecAdmin

Java Operators MCQs (Multiple Choice Questions)

Java, one of the most widely used programming languages, offers a rich set of operators to perform various operations, from basic arithmetic to complex logical evaluations. Understanding these operators and their precedence is crucial for anyone looking to master Java programming. To aid in this journey, we have compiled a meticulously curated set of multiple-choice questions (MCQs) focusing on Java Operators. This collection spans from the foundational arithmetic and relational operators to the more nuanced conditional and bitwise operators. Whether you’re a novice programmer aiming to solidify your Java fundamentals or an experienced developer looking to brush up on the intricacies of Java operators, this guide is tailored to enhance your knowledge and test your skills.

Q1 Which operator is used to compare two values for equality in Java?

A = B == C === D <>

Q2 What is the output of the expression ’10 % 3′?

A 3 B 1 C 0 D 10

Q3 Which operator is used for string concatenation in Java?

A + B & C && D .

Q4 What does the ‘++’ operator do to a variable?

A Decreases its value by 1 B Increases its value by 1 C Doubles its value D Squares its value

Q5 Which of the following is the correct way to use the ternary operator?

A var result = (condition) ? valueIfTrue : valueIfFalse; B var result = (condition, valueIfTrue, valueIfFalse); C var result = ?(condition) valueIfTrue : valueIfFalse; D var result = if(condition) ? valueIfTrue : valueIfFalse;

Q6 What is the result of the expression ‘5 == 5’?

A true B false C 1 D 0

Q7 Which operator checks if two variables refer to the same object instance?

A == B === C equals() D instanceof

Q8 What is the result of the expression ‘!true’?

Q9 What is the precedence order of the operators: *, +, and ()?

A +, *, () B *, +, () C (), *, + D (), +, *

Q10 Which operator is used to invert the value of a boolean variable?

A ~ B ! C – D ^

Q11 Which of the following operators has the highest precedence in Java?

A && B + C / D ++

Q12 What does the expression ‘x &= y’ do?

A Sets x to the result of x AND y B Sets x to the result of x OR y C Sets x to the result of x XOR y D Compares x and y for equality

Q13 Which of the following is true about the ‘instanceof’ operator?

A It is used to compare numerical values B It is used for arithmetic operations C It checks if an object is an instance of a specific class or interface D It concatenates strings

Q14 Which of the following is a unary operator in Java?

A + B – C / D *

Q15 What is the output of ‘1 << 2'?

A 1 B 2 C 3 D 4

Q16 What is the output of the following code snippet?

A 15 B 16 C 17 D 18

Q17 How do you perform a bitwise OR operation between two integers x and y?

A x || y B x | y C x OR y D x + y

Q18 Which operator is used for type casting in Java?

A cast B instanceof C () D typeof

Q19 What is the result of the expression ‘null instanceof Object’?

A true B false C NullPointerException D Compilation error

Q20 What does the expression ‘var a = 20; var b = a << 2;' set 'b' to?

A 5 B 40 C 80 D 100

Q21 Which of the following is NOT a relational operator in Java?

A B >= C == D =>

Q22 What is the use of the operator ‘?:’ in Java?

A To perform logical operations B To perform exponential calculations C To assign a value based on a condition D To concatenate strings

Q23 What does the ‘>>>=’ assignment operator do?

A Performs a right shift and assigns the result B Performs a left shift and assigns the result C Performs an unsigned right shift and assigns the result D Performs an unsigned left shift and assigns the result

Q24 Which operator is used to determine if an object is of a certain type (class) at runtime?

A == B != C instanceof D getClass()

Q25 What does the expression ‘true || false’ evaluate to in Java?

A true B false C 0 D 1

Q26 Which of the following is the correct use of the bitwise XOR operator in Java?

A int result = x ~ y; B int result = x ^ y; C int result = x | y; D int result = x && y;

Q27 What is the result of the following operation: ‘4 | 3’?

A 0 B 7 C 1 D 12

Q28 In Java, what does the ‘>>>=’ operator do?

A Performs a right arithmetic shift and assigns the result B Performs a left arithmetic shift and assigns the result C Performs an unsigned right shift and assigns the result D Performs an unsigned left shift and assigns the result

Navigating through the realm of Java operators is a fundamental step towards mastering Java programming. The provided MCQs offer a panoramic view of Java’s operator capabilities, from manipulating numbers and strings to making complex decisions based on various conditions. Engaging with these questions not only reinforces theoretical concepts but also sharpens practical problem-solving skills. As you progress, remember that the understanding of operators extends beyond their individual functionality; it’s about recognizing their role in the broader context of Java programming paradigms. We encourage learners to delve deeper into each topic, experiment with code examples, and continuously challenge their understanding. This exploration is not just about passing a test; it’s about laying a solid foundation for your Java programming journey.

Related Posts

How to configure static ip address on ubuntu 24.04, how to clone git tags, how to resolve error 1040 (08004): too many connections in mysql.

Save my name, email, and website in this browser for the next time I comment.

Type above and press Enter to search. Press Esc to cancel.

  •  About
  •  Contact

Object Oriented Programming - I (3140705)  MCQs

Mcqs of  introduction to java and elementary programming.

assignment operators are more efficiently implemented mcq

Javacodepoint

Java MCQs – Operators and Expressions

This article presents a series of Multiple-Choice Questions (MCQs) that will test your knowledge of Java operators and expressions. Whether you are a beginner looking to reinforce your understanding or an experienced developer sharpening your skills, these MCQs will help you deepen your comprehension of this crucial aspect of Java programming.

As a Java developer, understanding operators and expressions is fundamental to writing efficient and effective code. Operators are symbols that perform specific operations on variables, constants, and expressions, while expressions are combinations of these elements that produce a single value.

1.) What is the result of the expression 2 + 3 * 2?

Answer: Option B

Explanation: In Java, the multiplication operator (*) has higher precedence than the addition operator (+).

2.) What is the result of the expression “Java” + “CodePoint”?

Answer: Option C

Explanation: In Java, the + operator is used for both addition (for numeric types) and concatenation (for strings). When used with strings, it concatenates the two strings together.

3.) What is the result of the expression 11 % 3?

Explanation: The % operator is the modulus operator, which gives the remainder when the left operand is divided by the right operand. So, 11 % 3 gives the remainder of 11 divided by 3, which is 2.

4.) What is the result of the expression 1 > 3 && 4 < 5?

Explanation: In Java, the “&&” operator is the logical AND operator. It returns true if both operands are true; otherwise, it returns false.

5.) Which operator is used to check if two values are not equal in Java?

Answer: Option A

Explanation: In Java, the “!=” operator is used to check if two values are not equal.

6.) What is the result of the expression 1 + 2 + “0”?

Explanation: In Java, when the “+” operator is used with a string and any other data type, it performs string concatenation. So, 1 + 2 is evaluated first as 3 (numeric addition), and then “0” (a string) is concatenated to the result, resulting in “30”.

7.) What is the result of the expression 2 + 2 * 2 / 2 – 2?

Explanation: In Java, the order of evaluation for arithmetic operations follows the rules of precedence. The multiplication and division operations have higher precedence than addition and subtraction. So, the expression is evaluated as follows: (2 + (2 * 2) / 2) – 2 = 2.

8.) What is the purpose of the “++” operator in Java?

Answer: Option D

9.) Which operator is used for logical AND in Java?

Explanation: In Java, the “&&” operator is used for logical AND.

10.) Which operator is used for assignment in Java?

Explanation: In Java, the “=” operator is used for assignment.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

  • Trending Now
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial

Operator Associativity in Programming

  • Assignment Operators in Programming
  • Binary Operators in Programming
  • Bitwise AND operator in Programming
  • Conditional Operator in Programming
  • Bitwise OR Operator (|) in Programming
  • Comparison Operators in Programming
  • What are Operators in Programming?
  • Logical AND operator in Programming
  • Unary Operators in Programming
  • Modulus Operator in Programming
  • Types of Operators in Programming
  • Increment and Decrement Operators in Programming
  • C++ Assignment Operator Overloading
  • Difference between Operator Precedence and Operator Associativity
  • Operator Precedence and Associativity in C
  • Precedence and Associativity of Operators in Python
  • Operator Overloading in C++
  • Operator Overloading in Dart
  • Assignment Operators in Python

Operator associative refers to the order in which operators of the same precedence are used in a word. In a programming language, it is important to understand the interactions between operators to properly define and test expressions. In this article, we will discuss operator associativity in programming.

Table of Content

  • Operator Associativity in Arithmetic Operators
  • Operator Associativity in Relational Operators
  • Operator Associativity in Logical Operators
  • Operator Associativity in Assignment Operators
  • Operator Associativity in Bitwise Operators
  • Operator Associativity in Conditional (Ternary) Operator
  • Operator Associativity in Unary Operators
  • Operator Associativity in C
  • Operator Associativity in C++
  • Operator Associativity in Java
  • Operator Associativity in Python
  • Operator Associativity in C#
  • Operator Associativity in Javascript

Here is a table illustrating the Operator Associativity in Programming:

Operator Associativity in Arithmetic Operators:

Mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and parameter (%), are usually symmetrical from left to right. This means that if there are multiple preceding functions in an expression, the processing is done from left to right .

int result = 5 + 3 * 2; // result will be 11 (5 + (3 * 2))

In the expression `5 + 3 * 2`, multiplication (*) including left-right association, takes higher priority and is considered first. So, `3 * 2` is `6`. The addition (+) is then performed, resulting in `5 + 6`, which equals `11`. This arrangement ensures that actions flow from left to right, mirroring the natural reading pattern. Left to right associations remain consistent, ensuring terms are evaluated as predicted without the need for clear parentheses to indicate the order Overall, it ensures clarity and simplicity in mathematical terms by an implicit sequence of activities based on the location of operators in a word.

Operator Associativity in Relational Operators:

Relational operators, like equality (==), inequality (!=), greater than (>), and less than (<), usually have left-to-right associativity as well.

boolean condition = 2 < 5 == true; // condition will be true ((2 < 5) == true)

In the case of relational expressions such as `2 < 5 == true`, the left-to-right association implies a left-to-right evaluation. So, they first check `2 < 5`, get `true`, then evaluate `true == true`, resulting in `true`. This chart ensures accuracy when comparing values ​​from left to right, and reflects a natural reading pattern. Left-to-right associations provide clarity by describing predictable behavioral patterns, conforming to observable expectations, and facilitating lexical analysis without the need for explicit parentheses and therefore `conditioning ` for `true`, which means that `2` is indeed less than `5`, and `true` is `true` It is equivalent.

Operator Associativity in Logical Operators:

Logical operators, including AND (&&) and OR (||), often exhibit left-to-right associativity.

bool_result = True or False and True # bool_result will be True (True or (False and True))

In logical terms such as `True or False and True`, a left-to-right association implies a left-to-right evaluation. Thus, `false and true` is the first analysis, which leads to `false`. A `true or false` evaluation is then performed, resulting in a `true` result. This process mirrors the natural reading process and ensures that logical positions are consistently explored from left to right. Left-to-right association provides clarity by defining a predictable order of execution, facilitates word analysis without the need for explicit parentheses. As a result, `bool_result` is assigned `True`, indicating that the first condition is true or the second condition evaluates to true.

Operator Associativity in Assignment Operators :

Assignment operators, like the simple assignment (=) and compound assignments (e.g., +=), typically have right-to-left associativity.

int a, b; a = b = 5; // Both a and b will be assigned the value 5 (b = 5, then a = b)

In applications like `a = b = 5`, the right-to-left association implies right-to-left analysis. As a result, the value `5` (`b = 5`) is first assigned to `b`, followed by the value `b` (`a = b`). This sequence ensures that `a` and `b` end up with the same value `5`. The right-to-left association simplifies the usage case by specifying an explicit usage pattern without the need for explicit parentheses, making the code readable and simple and thus providing `5` both `a` and `b`, making it a shorter and more efficient chaining function.

Operator Associativity in Bitwise Operators :

Bitwise operators, such as AND (&), OR (|), and XOR (^), usually have left-to-right associativity.

let result = 1 | 2 & 3; // result will be 3 (1 | (2 & 3))

`1 | 2 & 3`, left-to-right association versus left-to-right analysis. Thus, `2 & 3` are considered first, and `2` is the result. Then, `1 | 2` is evaluated, resulting in `3`. Left-to-right association ensures consistent analysis of bitwise functions while providing clarity in reference semantics. This association simplifies Bitwiz vocabulary by providing an explicit sequence of functions without the need for explicit parentheses. Consequently, a `result` is assigned to `3`, indicating a bitwise OR operation between `1` and `2`, after a bitwise AND operation between `2` and `3`.

Operator Associativity in Conditional (Ternary) Operator :

The conditional operator (?:) has right-to-left associativity.

let max = (a > b) ? a : b; // max will be the greater of a and b

The conditional function `(a > b) ? a : b`, right-to-left association versus right-to-left analysis. Consequently, if `a` is greater than `b`, then `a` is chosen; otherwise `b` is selected. Right-to-left association simplifies conditional terminology, and suggests a clear order of operation without the need for explicit parentheses. This association ensures that the conditional term is correctly evaluated, whereby the condition is first evaluated, followed by the selection of `a` or `b` depending on the condition and thus the `max` value of a higher between `a` and ` b`, in order to provide for shortened appointments Effective and conditional appointments.

Operator Associativity in Unary Operators :

Unary operators, like negation (-) and logical NOT (!), often have right-to-left associativity.

result = -(-5); # result will be 5 (-(-5))

In single expressions such as `-(-5)`, the right-to-left association implies right-to-left analysis. Initially, the internal negation `-5` is computed, resulting in `-(-5)` equal to `5`. Right-to-left association ensures that external negation acts on the consequences of internal negation. As a result, `-(-5)` evaluates to `5`, indicating that a negation is rejected. This association simplifies single terms and indicates a clear sequence of functions without the need for explicit parentheses. Thus, the resulting value is `5`, because the outer negation eventually negates the result, resulting in the original value

Operator Associativity in C:

Here are the implementation of Operator Associativity in C language:

Operator Associativity in C++:

Here are the implementation of Operator Associativity in C++ language:

Operator Associativity in Java:

Here are the implementation of Operator Associativity in java language:

Operator Associativity in Python:

Here are the implementation of Operator Associativity in python language:

Operator Associativity in C#:

Here are the implementation of Operator Associativity in C# language:

Operator Associativity in Javascript:

Here are the implementation of Operator Associativity in javascript language:

Conclusion:

Operator associativity determines the order in which operators of the same precedence are evaluated in an expression. In simple terms, it decides whether operators are evaluated from left to right or from right to left. This helps clarify the sequence of operations in complex expressions and ensures consistency in the behavior of operators.

Please Login to comment...

Similar reads.

  • Programming

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Welcome to the Operators and Control Statements MCQs Page

Dive deep into the fascinating world of Operators and Control Statements with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Operators and Control Statements , a crucial aspect of Java Programming . In this section, you will encounter a diverse range of MCQs that cover various aspects of Operators and Control Statements , from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Java Programming .

Check out the MCQs below to embark on an enriching journey through Operators and Control Statements . Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Java Programming .

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Operators and Control Statements . You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Operators and Control Statements MCQs | Page 1 of 38

Explore more topics under java programming, language fundamentals, data types,variables and arrays, classes and methods, inheritance, string handling, java lang and java io, serialization and networking, exception handling in java, multithreading, io and applets, event handling, interfaces and packages, session management,jsp and servlet, miscellaneous topics in java.

Both Numeric & Characters

Floating – point numbers

Both Integers and floating – point numbers

None of the mentioned

1, 2 & 3

1, 2, 3 & 4

Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms

Assignment operators run faster than their equivalent long forms

Assignment operators can be used only with numeric and character data type

5.640000000000001 5

5.640000000000001 5.0

5 5.640000000000001

<<<

>>>

Suggested Topics

Are you eager to expand your knowledge beyond Java Programming? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!

assignment operators are more efficiently implemented mcq

Hone your computational mathematics skills with our MATLAB MCQs. Understand...

assignment operators are more efficiently implemented mcq

Artificial Intelligence

Get started on the path to the future with our Artificial Intelligence MCQs. Covering...

assignment operators are more efficiently implemented mcq

Management Information Systems

Discover how businesses leverage technology with our Management Information Systems...

assignment operators are more efficiently implemented mcq

Master the suite of productivity tools with our MS Office MCQs. From Word and Excel...

assignment operators are more efficiently implemented mcq

Computer Graphics

Step into the visual side of computing with our Computer Graphics MCQs. Cover topics...

assignment operators are more efficiently implemented mcq

Wireless and Mobile Communications

Get to grips with modern communication technologies with our Wireless and Mobile...

assignment operators are more efficiently implemented mcq

Cryptography and Network Security

Secure your knowledge of information security with our Cryptography and Network...

assignment operators are more efficiently implemented mcq

C Programming

Master one of the most widely used programming languages with our C Programming MCQs....

assignment operators are more efficiently implemented mcq

Web Technologies

Master the building blocks of the web with our Web Technologies MCQs. From HTML and...

IMAGES

  1. Assignment Operators in Java with Examples

    assignment operators are more efficiently implemented mcq

  2. Comparison Operator With Assignment Operator

    assignment operators are more efficiently implemented mcq

  3. PPT

    assignment operators are more efficiently implemented mcq

  4. Assignment Operators in C Detailed Explanation

    assignment operators are more efficiently implemented mcq

  5. Assignment Operators.pdf

    assignment operators are more efficiently implemented mcq

  6. Assignment Operators in C with Examples

    assignment operators are more efficiently implemented mcq

VIDEO

  1. NTT and Credit Agricole

  2. Implementation Preference Quiz Solution

  3. Core

  4. complete typescript operators ternary operator with mcQ#YouTube #yt shorts#web developer

  5. P.I.L.L. Method Explained!

  6. CS202 Assignment 1 Solution Spring 2024

COMMENTS

  1. Arithmetic Operators

    a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. b) Assignment operators run faster than their equivalent long forms. c) Assignment operators can be used only with numeric and character data type. d) None of the mentioned. View Answer.

  2. Java MCQ Questions

    A. Assignment operators can be used only with numeric and character data type B. Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms C. Assignment operators run faster than their equivalent long forms D. None of the mentioned. View Answer

  3. Java Part 2: Questions on Operators and Control Statements

    a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. b) Assignment operators run faster than their equivalent long forms. c) Assignment operators can be used only with numeric and character data type. d) None

  4. JAVA-Operators and Control Statements Flashcards

    Which of these statements are incorrect? a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. b) Assignment operators run faster than their equivalent long forms. c) Assignment operators can be used only with numeric and character data type. d) None.

  5. Assignment Operators in Programming

    Assignment operators are used in programming to assign values to variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...

  6. Assignment Operators & Operator Precedence Questions & Answers

    View Answer. 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. 6.

  7. Java Multiple Choice Questions (MCQ)

    View Answer. 5. Which of these statements are incorrect? A). Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. B). Assignment operators run faster than their equivalent long forms. C). Assignment operators can be used only with numeric and character data type.

  8. Why are arithmetic assignment operators more efficient?

    An example of thee shorthand Java Arithmetic operator is a += 4; for a=a+4; In The Complete Reference, Java 2, Herbert Schildt mentions "they are implemented more efficiently by the Java run-time system than are their equivalent" What makes its implementation more efficient than a=a+4;

  9. Java Assignment Operators with Examples

    variable operator value; Types of Assignment Operators in Java. The Assignment Operator is generally of two types. They are: 1. Simple Assignment Operator: The Simple Assignment Operator is used with the "=" sign where the left side consists of the operand and the right side consists of a value. The value of the right side must be of the same data type that has been defined on the left side.

  10. 4.5: Assignment Operator

    The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated with the concept of moving a value into the storage location (again usually a variable). Within C++ programming language the symbol used is the equal symbol.

  11. 1.4. Expressions and Assignment Statements

    In this lesson, you will learn about assignment statements and expressions that contain math operators and variables. 1.4.1. Assignment Statements ¶. Remember that a variable holds a value that can change or vary. Assignment statements initialize or change the value stored in a variable using the assignment operator =.

  12. PRO192 Flashcards

    a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. b) Assignment operators run faster than their equivalent long forms. c) Assignment operators can be used only with numeric and character data type. d) None

  13. Java Operators MCQs (Multiple Choice Questions)

    Show Answer. Check Answer. Navigating through the realm of Java operators is a fundamental step towards mastering Java programming. The provided MCQs offer a panoramic view of Java's operator capabilities, from manipulating numbers and strings to making complex decisions based on various conditions. Engaging with these questions not only ...

  14. MCQs of Introduction to java and elementary programming

    MCQs of Introduction to java and elementary programming. ... Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms (b) Assignment operators run faster than their equivalent long forms ... Assignment operators can be used only with numeric and character data type (d) None of the mentioned ...

  15. Java MCQs

    As a Java developer, understanding operators and expressions is fundamental to writing efficient and effective code. Operators are symbols that perform specific operations on variables, constants, and expressions, while expressions are combinations of these elements that produce a single value. 1.) What is the result of the expression 2 + 3 * 2?

  16. What Are Assignment Operators

    Learning about assignment operators is foundational to programming. They allow us to: - Initiate and change variable values. - Create interactive and responsive programs. - Write more concise and readable code. They are not just symbols on the screen; they pave the way toward mastering programming logic and efficiency.

  17. Java Programming Multiple choice Questions and Answers-Operators and

    a. Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. b. Assignment operators run faster than their equivalent long forms. c. Assignment operators can be used only with numeric and character data type. d. None of the mentioned. View Solution.

  18. Operator Associativity in Programming

    Operator Associativity in Logical Operators: Logical operators, including AND (&&) and OR (||), often exhibit left-to-right associativity. bool_result = True or False and True # bool_result will be True (True or (False and True)) In logical terms such as `True or False and True`, a left-to-right association implies a left-to-right evaluation.

  19. SOLVED: Which of the statements are incorrect?: Assignment operators

    2. "Assignment operators are more efficiently implemented by java run-time system than their equivalent long forms" - This statement is correct. Assignment operators are optimized by the Java run-time system, making them more efficient than their long forms. Step 3/4 3.

  20. Which of these statements are incorrect?

    Which of these statements are incorrect? Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms Assignment operators run faster than their equivalent long forms Assignment operators can be used only with numeric and character data type None of the mentioned. Java Programming Objective type Questions and Answers.

  21. Which of these statements are incorrect?

    Which of these statements are incorrect? (a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. (b) Assignment operators run faster than their equivalent long forms. (c) Assignment operators can be used only with numeric and character data type. (d) None of the mentioned.

  22. Which of the statements are incorrect?: Assignment operators can be

    Assignment operators are more efficiently implemented by java run-time system than their equivalent long forms. A specific example of the assignment operator where the source that is there on the right-hand side and destination which is there on the left-hand side are of the same class type is the copy assignment operator.

  23. Java Programming Multiple choice Questions and Answers-Operators and

    a. Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. b. Assignment operators run faster than their equivalent long forms. c. Assignment operators can be used only with numeric and character data type. d. None of the mentioned. View Solution.