Mock exam 2
1)What is the result of attempting to compile and run the program ?.
Class C {
public static void main(String[] args) {
int[]a1[]=new int[3][3]; //3
int a2[4]={3,4,5,6}; //4
int a2[5]; //5
}}
1.compiletime error at lines 3,4,5
2.compiltime error at line 4,5
3.compiletime error at line 3
4.Runtime Exception
5.None of the above
Ans: 2
Explanation:
no value shoud be specified in the rightsidebrackets when constructing an array
2)which lines generate compile time errors?
interface I{
void f1(); // 1
public void f2(); // 2
protected void f3(); // 3
private void f4(); // 4
}
1.compiletime error at lines 1,2,3,4
2.compiletime error at line 3
3.compiletime error at line 1
4.compiletime error at lines 3,4
5.None of the above
Answer: 4
Explanation:
all methods declared within an interface are implicitly public, a weaker access level can not be declared.
3)which lines generate compile time errors?
class C{
int i;
public static void main (String[] args) {
int i; //1
private int a = 1; //2
protected int b = 1; //3
public int c = 1; //4
System.out.println(a+b+c); //5
}}
1.compiletime error at lines 1,2,3,4,5
2 compiletime error at lines 2,3,4,5
3.compiletime error at lines 2,3,4
4.prints 3
5.None of the above
Answer 2
Explanation:
The access modifiers public, protected and private, can not be applied to variables declared inside methods.
4)What is the result of attempting to compile and run the program?
class C {
public static void main (String[] a1) {
System.out.print(a1[1] + a1[2] + a1[3]);
}}
java command A B C
1.Prints: ABC
2.Prints BC and Runtime Exception
3.Prints: BCD
4.Runtime Exception
5.None of the above
Answer 2
Explanation:
array index outof bounds exception only till a1[2] is allowed.
5)What is the result of attempting to compile and run the program?
class C{
static int s;
public static void main(String a[]){
C obj=new C();
obj.m1();
System.out.println(s);
}
void m1();
{
int x=1;
m2(x);
System.out.println(x+"");
}
void m2(int x){
x=x*2;
s=x;
}}
1.prints 1,2
2.prints 2,0
3.prints 2,2
4.compile time error
5.Noneofthe above
Answer: 1
Explanation:
Only objects and arrays are passed by reference.other are passed by value.s is a static variable which is global to the class
6)What is the result of attempting to compile and run the program?
class C {
public static void main(String[] args) {
int i1=1;
switch(i1){
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}}}
1.prints one two three
2.prints one
3.compile time error
4.Runtime exceptionf
5.None of the above
Answer: 1
Explanation:
There is no break statement in case 1 so it causes the below case statements to execute regardless of their values
more
Class C {
public static void main(String[] args) {
int[]a1[]=new int[3][3]; //3
int a2[4]={3,4,5,6}; //4
int a2[5]; //5
}}
1.compiletime error at lines 3,4,5
2.compiltime error at line 4,5
3.compiletime error at line 3
4.Runtime Exception
5.None of the above
Ans: 2
Explanation:
no value shoud be specified in the rightsidebrackets when constructing an array
2)which lines generate compile time errors?
interface I{
void f1(); // 1
public void f2(); // 2
protected void f3(); // 3
private void f4(); // 4
}
1.compiletime error at lines 1,2,3,4
2.compiletime error at line 3
3.compiletime error at line 1
4.compiletime error at lines 3,4
5.None of the above
Answer: 4
Explanation:
all methods declared within an interface are implicitly public, a weaker access level can not be declared.
3)which lines generate compile time errors?
class C{
int i;
public static void main (String[] args) {
int i; //1
private int a = 1; //2
protected int b = 1; //3
public int c = 1; //4
System.out.println(a+b+c); //5
}}
1.compiletime error at lines 1,2,3,4,5
2 compiletime error at lines 2,3,4,5
3.compiletime error at lines 2,3,4
4.prints 3
5.None of the above
Answer 2
Explanation:
The access modifiers public, protected and private, can not be applied to variables declared inside methods.
4)What is the result of attempting to compile and run the program?
class C {
public static void main (String[] a1) {
System.out.print(a1[1] + a1[2] + a1[3]);
}}
java command A B C
1.Prints: ABC
2.Prints BC and Runtime Exception
3.Prints: BCD
4.Runtime Exception
5.None of the above
Answer 2
Explanation:
array index outof bounds exception only till a1[2] is allowed.
5)What is the result of attempting to compile and run the program?
class C{
static int s;
public static void main(String a[]){
C obj=new C();
obj.m1();
System.out.println(s);
}
void m1();
{
int x=1;
m2(x);
System.out.println(x+"");
}
void m2(int x){
x=x*2;
s=x;
}}
1.prints 1,2
2.prints 2,0
3.prints 2,2
4.compile time error
5.Noneofthe above
Answer: 1
Explanation:
Only objects and arrays are passed by reference.other are passed by value.s is a static variable which is global to the class
6)What is the result of attempting to compile and run the program?
class C {
public static void main(String[] args) {
int i1=1;
switch(i1){
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}}}
1.prints one two three
2.prints one
3.compile time error
4.Runtime exceptionf
5.None of the above
Answer: 1
Explanation:
There is no break statement in case 1 so it causes the below case statements to execute regardless of their values
more