2)
interface I{
void f1(); // 1
public void f2(); // 2
protected void f3(); // 3
private void f4(); // 4
}
which lines generate compile time errors?
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)
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)
class C {
public static void main (String[] a1) {
System.out.print(a1[1] + a1[2] + a1[3]);
}}
What is the result of attempting to compile and run the program?
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)
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)
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");
}}}
What is the result of attempting to compile and run the program?
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
7)
Each element must be unique
Duplicate elements must not replace old elements.
Elements are not key/value pairs.
Accessing an element can be almost as fast as performing a similar operation on an array.
Which of these classes provide the specified features?
1.LinkedList
2.TreeMap
3.HashMap
4.HashSet
5.None of the above
Answer: 4
8)
class C1
{
static interface I
{
static class C2
{
}
}
public static void main(String a[])
{
C1.I.C2 ob1=new C1.I.C2();
System.out.println("object created");
}
}
What is the result of attempting to compile and run the program?
1.prints object created
2.Compile time error
3.Runtime Excepion
4.None of the above
Answer: 1
Explanation:
A static interface or class can contain static members.Static members can be accessed without instantiating the particular class
9)
class C1
{
static class C2
{
static int i1;
}
public static void main(String a[])
{
System.out.println(C1.C2.i1);
}
}
What is the result of attempting to compile and run the program?
1.prints 0
2.Compile time error
3.Runtime exception
4.None of the above
Answer: 1
Explanation:
static members can be accessed without instantiating the particular class
10)
A signed data type has an equal number of non-zero positive and negative values available
1.true
2.false
Answer: 2
Explanation:
The range of negative numbers is greater by 1 than the range of positive numbers