Skip to content
doubtly

What will be the output of the following Java program?

What will be the output of the following Java program?

  1.     class leftshift_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              byte x = 64;
  6.              int i;
  7.              byte y; 
  8.              i = x << 2;
  9.              y = (byte) (x << 2)
  10.              System.out.print(i + " " + y);
  11.         } 
  12.     }

a) 0 256
b) 0 64
c) 256 0
d) 64 0

Doubtly Asked Updated Student doubt Save WhatsApp Report

2 answers

Answer: (c) 256 0

(The statement y = (byte) (x << 2) is missing a semicolon in the question; with it added, the program runs as below.)

  • x is a byte with value 64 = 0100 0000 in binary.
  • In x << 2, Java first promotes the byte to int, then shifts left by 2: \(64\times2^{2} = 256\). So i = 256.
  • 256 in binary is 1 0000 0000. Casting to byte keeps only the lowest 8 bits, which are 0000 0000. So y = 0.

The program prints 256 0.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.