1.
What is the output of the following program?

class MyClass
{
int var;
int getVar()
{
return var;
}

public static void main(String[] args)
{
System.out.println(getVar()+1);
}
}

(a) 0
(b) 1
(c) compilation error
(d) runtime error

Answer
(c) Main is a static method and static methods can not access the non-static members of a class.

2.
What is the output of the following program?

class MyClass
{
int var;
static int getVar()
{
return var;
}

public static void main(String[] args)
{
System.out.println(getVar()+1);
}
}

(a) 0
(b) 1
(c) compilation error
(d) runtime error

Answer
(c) The method getVar() can not access var
C:\java_practice\src\static\MyClass.java:6: error: non-static variable var cannot be referenced from a static context
return var;
^
1 error

3.
What is the output of the following program?

class MyClass
{
static int i=10;
static int j=i;

public static void main(String[] args)
{
System.out.println(“i=”+i);
System.out.println(“j=”+j);
}
}

(a) i=10
j=10
(b) i=10
j=0
(c) compilation error
(d) none of the above

Answer
(a) The value of static variable i is assigned to j.

4.
What is the output of the following program?

class MyClass
{
static int j=i;
static int i=10;
public static void main(String[] args)
{
System.out.println(“i=”+i);
System.out.println(“j=”+j);
}
}

(a) i=10
j=10
(b) i=10
j=0
(c) compilation error
(d) none of the above

Answer
(d) None of the Above since The compiler throws the “Illegal Forward Reference” Error.
C:\java_practice\src\static\MyClass.java:3: error: illegal forward reference
static int j=i;
^
1 error

5.
What is the output of the following program?

class MyClass
{
static char i;
static int j;

public static void main(String[] args)
{
System.out.println(“i=”+i+”,j=”+j);
}
}

(a) i=0,j=0
(b) i=null,j=0
(c) i= ,j=0
(d) Compilation Error

Answer
(c) i= j=0
The default value of char is ” ” and for integer is 0

6.
What is the output of the following program?

class MyClass
{
int i;
float j;

public static void main(String[] args)
{
System.out.println(“i=”+i+”j=”+j);
}
}

(a) i=0 j=0.0
(b) i=0 j=0.000000
(c) none of the above
(d) Compilation Error

Answer
(d) main is a static method and so it cannot access non static member variables

7.
What is the output of the following program?

class MyClass
{
static int myStaticVar;
static void func1()
{
myStaticVar++;
System.out.println(“Inside func1”);
}
static void func2()
{
myStaticVar++;
System.out.println(“Inside func1”);
}
public static void main(String[] args)
{
System.out.println(myStaticVar);
func1();
System.out.println(myStaticVar);
func2();
System.out.println(myStaticVar);
}
}

(a) 0
Inside func1
1
Inside func1
2
(b) 0
Inside func1
0
Inside func1
0
(c) none of the above
(d) Compilation Error

Answer
(a) All the functions have access to the static variable myStaticVar and can change it directly.
0
Inside func1
1
Inside func1
2

8.
What is the output of the following program?

class MyClass
{
static int i=10;

public static void main(String[] args)
{
int i=20;
System.out.println(i);
System.out.println(MyClass.i);
}
}

(a) 20
20
Because changing local variable changes the value of static variable
(b) 20
10
Because the local i is local to main() and static i is still 10
(c) Compilation Error
Because You can not re-declare the same variable

(d) 10
10
Because both the “i” in println refers to the static member variable i

Answer
(b) 20
10
Because the local i is local to main() and static i is still 10.
The static variable can be accessed by using the class name directly in this case.
Syntax :
ClassName.variableName
9.
Which of these cannot be declared static?
(a) class
(b) instance
(c) variable
(d) method

Answer
(c) static statements are run as soon as class containing then is loaded, prior to any object declaration.

10.
What is Math.floor(3.6)?
(a) 3.0
(b) 3
(c) 4.0
(d) 4

Answer
(a) The method floor gives the largest integer that is less than or equal to the argument.
static keyword in Java
Tagged on: