and CORRECT Answers
What will this small program output?
class Main {
public static void foo() {
System.out.println(x);
}
public static int x = 42;
public static void main(String[] args) {
x = 6;foo();
}
} - CORRECT ANSWER - 6
What is the output of this Java program?
class Driver {
public static void main(String[] args) {
int a = foo(3);
int b = bar(4);
}
static int foo(int a) {
a = bar(a + 1);
System.out.print(a);
return a;
}
static int bar(int a) {
,System.out.print(a);
return a - 1;
}
} - CORRECT ANSWER - 434
What is the output of this Java program?
class Driver {
public static void main(String[] args) {
int a = 2;
int b = 1;
int c = 2;
int x = 1;
if (a > 1) {
x = x + 200;
}
if (b > 4) {
x = x + 10;
}
if (c > 7) {
x = x + 2;
}
System.out.print(x);
}
} - CORRECT ANSWER - 201
Consider the following operations on a stack. What will be the final output?
, Stack<Integer> stack = new Stack<();
stack.push(10);
stack.push(20);
stack.pop();
stack.push(30);
stack.push(40);
stack.pop();
stack.push(stack.peek());
System.out.println(stack.pop() + stack.pop() + stack.peek()); - CORRECT ANSWER - 70
Evaluate the following code to determine the output. Hint - ASCII value of A is 65.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Character> list = new ArrayList<>();
for (char c = 'B'; c <= 'D'; c++) list.add(c);
for (int i = 0; i < list.size(); i++) {
list.set(i, (char)(list.get(i) + 2));
}
list.add((char)(65 + 1));
list.remove(1);
list.remove(Integer.valueOf('C'));
list.add(0, (char)(list.get(1) - 1));
System.out.println((int)list.get(1));\ }} - CORRECT ANSWER - 68
What is the output of the following code?