0% found this document useful (0 votes)
2 views7 pages

Java Output Questions

The document presents a series of Java code questions along with their expected outputs. Each question involves different Java concepts such as conditionals, loops, and arithmetic operations. The outputs range from simple integers to more complex sequences and lists, highlighting various aspects of Java programming.

Uploaded by

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

Java Output Questions

The document presents a series of Java code questions along with their expected outputs. Each question involves different Java concepts such as conditionals, loops, and arithmetic operations. The outputs range from simple integers to more complex sequences and lists, highlighting various aspects of Java programming.

Uploaded by

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

Java Output Questions

Q1: What will be the output of the following code?

int a = 5, b = 5;

if (a < b)

return function(b, a);

else if (b != 0)

return a * function(a, b - 1);

else

return 0;

// Output: 120

Q2: What will be the output of the following code?

char c = 'a';

[Link]("%d", (int)c);

// Output: 97

Q3: What will be the output of the following code?

char c = 'f', a = 's', b = 'x';

int sum = c + a + b;

[Link](sum);

// Output: 327

Q4: What will be the output of the following code?

int[] arr = {1, 2, 3, 4, 5};

for (int i = 0; i < [Link] - 1; i++) {

arr[i] = arr[i] + arr[i + 1];

[Link]([Link](arr));
// Output: [3, 5, 7, 9, 5]

Q5: What will be the output of the following code?

int f = fun(5, 7);

[Link](f);

public static int fun(int x, int y) {

if (x <= 0)

return y;

else

return fun(x - 1, y - 1);

// Output: 2

Q6: What will be the output of the following code?

int LIMIT = 10;

boolean[] primes = new boolean[LIMIT];

[Link](primes, true);

for (int i = 2; i < LIMIT; i++)

if (primes[i])

for (int j = i; i * j < LIMIT; j++)

primes[i * j] = false;

int z = 1;

for (int i = 2; i < LIMIT; i++)

if (primes[i])

[Link](z++ + "th prime = " + i);

// Output: List of primes below 10 with index

Q7: What will be the output of the following code?

int a = func(5);
[Link](a++);

public static int func(int a) {

return a--;

// Output: 5

Q8: What will be the output of the following code?

int val = 5;

do {

val++;

++val;

} while (val++ > 7);

[Link](val);

// Output: 10

Q9: What will be the output of the following code?

int m = 0;

if (m == 0) {

m = ((5 > 0 ? (m = 3) : 0), m = 1);

[Link](m);

} else {

[Link]("Test");

// Output: 1

Q10: What will be the output of the following code?

int i = 0;

while ((+(+i--)) != 0)

i = i + 5;
[Link](i);

// Output: -1

Q11: What will be the output of the following code?

[Link](func(35));

public static int func(int n) {

int i = 0;

while (n % 10 != 0) {

n = n + 3;

i++;

n = n - i;

return n;

// Output: 47

Q12: What will be the output of the following code?

int j = 0;

for (int i = 0; i <= 5; i++)

j = func(i);

[Link](j);

public static int func(int no) {

// static variable in Java

final int[] count = {0};

count[0] = count[0] + no;

return count[0];

// Output: Not directly translatable to Java. Use a static field or workaround.


// Let's say Output: 15

Q13: What will be the output of the following code?

int a = 5, b = 2, c = 1;

if (b > a && a > c && c > b)

b = a + 1;

else

a = b + 1;

[Link](a + b + c);

// Output: 8

Q14: What will be the output of the following code?

int x = 2, y = 1;

int z = x ^ y;

[Link](z);

// Output: 3

Q15: What will be the output of the following code?

int value = 1, n = 45;

while (value <= n)

value = value << 1;

[Link](value);

// Output: 64

Q16: What will be the output of the following code?

int x = 15, y = 12;

y = x - 1;

do {

[Link](x);

x = y + (x - 2);
} while (x < 40);

// Output: Sequence of x values until x >= 40

Q17: What will be the output of the following code?

int x = 2, y = 1;

int z = x & y;

[Link](z);

// Output: 0

Q18: What will be the output of the following code?

int value = 32, n = 1;

while (value >= n)

value = value >> 1;

[Link](value);

// Output: 0

Q19: What will be the output if x = 1 and y = 2?

solve(1, 2);

public static void solve(int x, int y) {

if (x > 1)

solve(x - 1, y + 3);

[Link](y);

// Output: 2

Q20: What will be the output if p = 4 and q = 2?

[Link](solve(4, 2));

public static int solve(int p, int q) {

int value;

while (q != 0) {
value = p % q;

p = q;

q = value;

return p;

// Output: 2

You might also like