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
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
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
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
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
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
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
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
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
Which of these cannot be declared static?
(a) class
(b) instance
(c) variable
(d) method Answer
10.
What is Math.floor(3.6)?
(a) 3.0
(b) 3
(c) 4.0
(d) 4