Java differentiates between how many types of variables? correct answers 4, instance, static,
local, and parameter variable
When the following code snippet is executed, what will be the output?
public void test() {
long l = 55;
int i = 44;
short s = 33;
byte b = 22;
i = (int) l;
s = (short) i;
b = (byte) s;
System.out.println("l = " + l);
System.out.println("i = " + i);
System.out.println("s = " + s);
System.out.println("b = " + b);
} correct answers l = 55
i = 55
s = 55
b = 55
If the following code snippet is executed, what will be the output?
public void test3()
{
int i = 50000;
short s = i;
} correct answers The program does not compile
When the following code snippet is executed, what will be the output?
public void test() {
int input = 7;
int output = ++input + ++input + ++input;
System.out.println(output);
} correct answers 27 (8 + 9 + 10)
When the following code snippet is executed, what will be the output?
public static void test5() {
int i = 4;
int j = 21;
int k = ++i * 7 + 2 - j--;
, System.out.println("k = " + k);
} correct answers k = 16 (5 * 7 + 2 - 21)
When the following code snippet is executed, what will be the output?
public void test2() {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.print("b = " + b);
System.out.println("c = " + c);
System.out.print("d = " + d);
} correct answers a = 2
b =3c = 4
d=1
In java static variables is used to correct answers - Refer to common properties to all objects
- Get a persistent value between different method calls
- It gets memory only once in a class area at the time of class loading
When the following code snippet is executed, what will be the output?
public class TestStatic {
int num1 = 6;
static int num2 = 10;
public static void main(String args[]) {
TestStatic s1 = new TestStatic();
TestStatic s2 = new TestStatic();
s1.num1 = 15;
s1.num2 = 17;
s2.num1 = 22;
s2.num2 = 28;
System.out.println(s1.num1 + " " + s1.num2 +
" " + s2.num1 + " " + s2.num2);
}
} correct answers 15 28 22 28
What will the given code result in System.out.printf("\n Java programming is very \"fun\" ");
correct answers Java programming is very "fun"