← All SCJP Flashcard Decks

Java Operators, Type Casting, and Assignments Flashcards

6 cards from real SCJP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 6 Java Operators, Type Casting, and Assignments flashcards as text
  1. What is the result of the expression `6 & 3` in Java?

    Answer: 2

    The & operator performs a bitwise AND. 6 in binary is 110 and 3 is 011. ANDing each bit: 110 & 011 = 010, which equals 2.

  2. What is the value of `byte b = (byte) 130;` in Java?

    Answer: -126

    byte holds values from -128 to 127. 130 in binary (8 bits) is 10000010. With a signed byte, the leading 1 indicates a negative number. Using two's complement, this equals -126.

  3. What does the compound assignment `x += 1.5;` do when x is declared as `int x = 3;`?

    Answer: Assigns 4 to x after an implicit narrowing cast

    Compound assignment operators include an implicit narrowing cast. `x += 1.5` is equivalent to `x = (int)(x + 1.5)`, so 3 + 1.5 = 4.5 is truncated to 4. A plain `x = x + 1.5` would fail to compile.

  4. What is the output of `System.out.println(1 + 2 + "3" + 4 + 5);`?

    Answer: 3345

    Evaluation is left-to-right. `1 + 2` = 3 (int addition), then `3 + "3"` = "33" (string concatenation), then "33" + 4 = "334", then "334" + 5 = "3345".

  5. What is the result of `System.out.println(5 >> 1);`?

    Answer: 2

    The >> operator is the signed right-shift. Shifting 5 (binary 101) right by 1 position gives 010, which is 2. Each right-shift by 1 is equivalent to integer division by 2.

  6. Which assignment causes a compile-time error?

    Answer: byte b = 100L;

    Assigning a long literal to a byte requires an explicit cast because it is a narrowing conversion. long→byte cannot be done implicitly. The other assignments are widening conversions (int→long, long→float) or a char-to-int widening, all of which are implicit.

How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy