0% found this document useful (0 votes)
6 views5 pages

Java Output Questions (For Practice)

The document contains a series of Java output questions along with their answer key, authored by Sir. Subhendu. Each question involves various Java programming constructs such as loops, conditionals, and arithmetic operations, culminating in specific outputs. The answer key provides the expected outputs for each question, facilitating understanding and verification of the code logic.

Uploaded by

gk27042009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Java Output Questions (For Practice)

The document contains a series of Java output questions along with their answer key, authored by Sir. Subhendu. Each question involves various Java programming constructs such as loops, conditionals, and arithmetic operations, culminating in specific outputs. The answer key provides the expected outputs for each question, facilitating understanding and verification of the code logic.

Uploaded by

gk27042009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Output Questions (with answer key)

By Sir. Subhendu

1.
int a = -7, b = ++a - a--;
boolean flag = (a++ > b--) || (a-- < ++b);
for(int i = 0; i < 2; i++, b+=a) {
a += b % 3;
}
if(flag) {
switch(a % 3) {
case 0: b += 10;
case 1: a -= 2; break;
default: a += b;
}
} else {
while(a < b) {
b -= ++a;
}
}
[Link](a + " " + b + " " + flag);

2.
double x = [Link](2, 3);
int n = (int)[Link](81);
while(n > 0 && x < 100) {
if(n % 2 == 0) x += n;
else x -= n;
n /= 2;
}
if(x > 50)
[Link]((int)x);
else
[Link]((int)(x + n));

3.
char ch = 'A';
int sum = 0;
boolean toggle = true;
for(int i = 0; i < 5; i++) {
sum += (int)ch;
if(toggle) ch += 2;
else ch--;
toggle = !toggle;
}
[Link](sum + " " + ch);
Java Output Questions (with answer key)
By Sir. Subhendu

4.
int count = -100;
for(int i = 1; i <= 4; i++) {
for(int j = 1; j <= 3; j++) {
if(i + j == 4) break;
if(i == j) continue;
count += i * j;
}
}
[Link](count);

5.
int a = 5, b = -a--;
boolean test = (a++ > b) && (--b < 0);
int count = 0;

while(a < 12) {


if(a % 2 == 0) {
b += a;
a++;
} else {
a += b % 3;
b--;
}
count++;
if(count > 5) break;
}
[Link](a + " " + b + " " + test + " " + count);

6.
int n = (int)[Link](9.7), sum = 0;
for(int i = 1; i <= 3; i++) {
n += i % 2 == 0 ? i * 2 : -i;
if(n % 3 == 0) sum += n;
else if(n % 2 == 0) sum -= n;
else sum += i;
}

switch(n % 5) {
case 0: sum += n; break;
case 1: sum -= 2 * n;
case 2: sum += 3;
default: sum++;
}

if(sum % 2 == 0) {
n = sum / 2;
} else {
Java Output Questions (with answer key)
By Sir. Subhendu

while(n > 0) {
sum -= n % 4;
n /= 2;
}
}
[Link](n + " " + sum);

7.
int x = 4, y = 9, k = 0;
while(y > x) {
for(int i = 1; i <= 2; i++) {
x += i;
y -= x % 3;
k += (x + y) % 5;
}
if(x % 2 == 0) y--;
else y += x;
}
[Link](x + " " + y + " " + k);

8.
char c = 'B';
int total = 0;
for(int i = 1; i <= 4; i++) {
int temp = 0;
for(int j = 0; j < i; j++) {
temp += c + i - j;
if(j % 2 == 0) c++;
else c--;
}
total += temp;
if(i % 2 == 0) c += 2;
else c--;
}
[Link](total + " " + c);

9.
int a = -3, b = 5;
boolean cond = (a++ + b--) > 0 ^ (--a < ++b);
int sum = 0;

[Link]("Initial: " + a + "," + b + "," + cond);

switch(cond ? 1 : 0) {
case 1: a += b; sum += a; break;
case 0: b -= a; sum -= b;
}
[Link]("After switch: " + a + "," + b + "," + sum);
Java Output Questions (with answer key)
By Sir. Subhendu

for(int i = 0; i < 1; i++) {


if(cond) {
b += i;
sum += b % 2;
} else {
a -= i;
sum -= a % 3;
}
[Link]("Loop " + i + ": " + a + "," + b + "," + sum);
}

while(a < b) {
sum += a * b;
b -= 2;
a++;
[Link]("While step: " + a + "," + b + "," + sum);
}
[Link]("Final: " + a + " " + b + " " + cond + " " + sum);

10.
int n = 45, sum = 0, steps = 0;

while(n > 0) {
if(n % 2 == 0) sum += n / 2;
else sum += n % 10;
n /= 3;
steps++;
}

for(int i = 1; i <= 3; i++) {


if(sum % i == 0) sum += i * i;
else sum -= i;
steps += i;
}

if(sum > 20) {


sum = (int)[Link](sum) + steps;
} else {
int temp = sum;
while(temp > 0) {
sum += temp % 5;
temp /= 2;
}
}

[Link]("Sum=" + sum);
[Link]("Steps=" + steps);
[Link]("Final=" + (sum + steps));
Java Output Questions (with answer key)
By Sir. Subhendu

Answer key:
Q. No. Output
1 -23 -15 true
2 4
3 333 E
4 -72
5 4 -2 true 6
6 05
7 10 9 9
8 698 F
9 Initial: -3,5,false
After switch: -3,8,-8
Loop 0: -3,8,-8
While step: -2,6,-32
While step: -1,4,-44
While step: 0,2,-48
While step: 1,0,-48
Final: 1 0 false -48
10 Sum=14
Steps=10
Final=24

You might also like