2. Question 1
An object is
Select 1 correct option.
a what classes are instantiated from.
b an instance of a class.
c a blueprint for creating concrete realization of abstractions.
d a reference to an attribute.
e a variable.
Ans: b
3. Question 2
What will the following code snippet print?
int index = 1;
String[] strArr = new String[5];
String myStr = strArr[index];
System.out.println(myStr);
Select 1 correct option.
A It will print nothing.
B It will print 'null'
C It will throw ArrayIndexOutOfBounds at runtime.
D It will print NullPointerException at runtime.
E None of the above.
Ans: b
4. Question 3
In which of these variable declarations, will the variable remain uninitialized
unless explicitly initialized?
Select 1 correct option.
a Declaration of an instance variable of type int.
b Declaration of a static class variable of type float.
c Declaration of a local variable of type float.
d Declaration of a static class variable of class Object
e Declaration of an instance variable of class Object.
Ans: c
5. Question 4
What is the range of a 'char' data type?
Select 1 correct option.
a 0 - 65, 535
b 0 - 65, 536
c -32,768 - 32,767
d 0 - 32,767
e None of the above
Ans: a
6. Question 5
What will the following program print?
public class TestClass
{
public static void main(String[] args)
{
unsigned byte b = 0;
b--;
System.out.println(b);
}
}
Select 1 correct option.
A0
B -1
C 255
D -128
E It will not compile.
Ans: e unsigned
7. Question 6
Carefully examine the following code.
public class StaticTest
{
static
{ System.out.println("In static"); }
{ System.out.println("In non - static"); }
public static void main(String args[ ])
{
StaticTest st1; //1
System.out.println(" 1 ");
st1 = new StaticTest(); //2
System.out.println(" 2 ");
StaticTest st2 = new StaticTest(); //3
}
}
What will be the output?
Select 1 correct option.
a In static, 1, In non - static, 2, In non - static : in that order.
b Compilation error.
c 1, In static, In non - static, 2, In non - static : in that order.
d In static, 1, In non - static, 2, In non - static : in unknown order.
e None of the above.
Ans: a
8. Question 7
What will happen when you compile and run the following program using the
command line:
java TestClass 1 2
public class TestClass
{
public static void main(String[] args)
{
int i = Integer.parseInt(args[1]);
System.out.println(args[i]);
}
}
Select 1 correct option.
a It'll print 1
b It'll print 2
c It'll print some junk value.
d It'll throw ArrayIndexOutOfBoundsException
e It'll throw NumberFormatException
Ans: d
9. Question 8
Which of the following are valid at line 1?
public class X
{
line 1: //put statement here.
}
Select 2 correct options
a String s;
b String s = 'asdf';
c String s = 'a';
d String s = this.toString();
e String s = asdf;
Ans: a&d
10. Question 9
A method is ..
Select 1 correct option.
a an implementation of an abstraction.
b an attribute defining the property of a particular abstraction.
c a category of objects.
d an operation defining the behavior for a particular abstraction.
e a blueprint for making operations.
Ans: d
11. Question 10
An instance member ...
Select 2 correct options
a can be a variable, constant or a method.
b is a variable or a constant.
c Belongs to the class.
d Belongs to an instance of the class.
e is same as a local variable.
Ans: a&d
12. Question 11
Given the following class, which of these given blocks can be inserted at line
1 without errors?
public class InitClass
{
private static int loop = 15 ;
static final int INTERVAL = 10 ;
boolean flag ;
//line 1
}
Select 4 correct options
a static {System.out.println("Static"); }
b static { loop = 1; }
c static { loop += INTERVAL; }
d static { INTERVAL = 10; }
e { flag = true; loop = 0; }
Ans: a,b,c,e
13. Question 12
Which of the following are correct ways to initialize the static variables MAX and CLASS_GUID ?
class Widget
{
static int MAX; //1
static final String CLASS_GUID; // 2
Widget()
{
//3
}
Widget(int k)
{
//4
}
}
Select 2 correct options
a Modify lines //1 and //2 as : static int MAX = 111; static final String CLASS_GUID = "XYZ123";
b Add the following line just after //2 : static { MAX = 111; CLASS_GUID = "XYZ123"; }
c Add the following line just before //1 : { MAX = 111; CLASS_GUID = "XYZ123"; }
d Add the following line at //3 as well as //4 : MAX = 111; CLASS_GUID = "XYZ123";
e Only option 3 is valid.
Ans: a,b
14. Question 13
What will be the result of attempting to compile and run the following class?
public class TestClass
{
public static void main(String args[ ] )
{
int i = 1;
int[] iArr = {1};
incr(i) ;
incr(iArr) ;
System.out.println( "i = " + i + " iArr[0] = " + iArr [ 0 ] ) ;
}
public static void incr(int n ) { n++ ; }
public static void incr(int[ ] n ) { n [ 0 ]++ ; }
}
Select 1 correct option.
a The code will print i = 1 iArr[0] = 1;
b The code will print i = 1 iArr[0] = 2;
c The code will print i = 2 iArr[0] = 1;
d The code will print i = 2 iArr[0] = 2;
e The code will not compile.
Ans: b
15. Question 14
Consider the following code snippet ...
boolean[] b1 = new boolean[2];
boolean[] b2 = {true , false};
System.out.println( "" + (b1[0] == b2[0]) + ", "+ (b1[1] == b2[1]) );
What will it print ?
Select 1 correct option.
a It will not compile.
b It will throw ArrayIndexOutOfBoundsError at Runtime.
c It will print false, true.
d It will print true, false.
e It will print false, false.
Ans: c
16. Question 15
What will the following program print?
public class TestClass
{
static String str;
public static void main(String[] args)
{
System.out.println(str);
}
}
Select 1 correct option.
a It will not compile.
b It will compile but throw an exception at runtime.
c It will print 'null'
d It will print nothing.
e None of the above.
Ans: c
17. Question 16
public class TestClass
{
public static void main(String[] args)
{
String tom = args[0];
String dick = args[1];
String harry = args[2];
}
}
What will the value of 'harry' if the program is run from the command
line:
java TestClass 111 222 333
Select 1 correct option.
a 111
b 222
c 333
d It will throw an ArrayIndexOutOfBoundsException
e None of the above.
Ans: c
18. Question 17
Which of these are keywords in Java?
Select 3 correct options
a default
b NULL
c String
d throws
e long
Ans: a,d,e
19. Question 18
Which of the following are valid identifiers?
Select 2 correct options
a class
b $value$
c angstrom
d 2much
e zer@
Ans: b,c
20. Question 19
public class TestClass
{
public static void main(String[] args)
{
String str = "111";
boolean[] bA = new boolean[1];
if( bA[0] ) str = "222";
System.out.println(str);
}
}
What will the above program print?
Select 1 correct option.
a 111
b 222
c It will not compile as bA[0] is uninitialized.
d It will throw an exception at runtime.
e None of the above.
Ans: a
21. Question 20
What will be the output of the following lines ?
System.out.println("" +5 + 6); //1
System.out.println(5 + "" +6); // 2
System.out.println(5 + 6 +""); // 3
System.out.println(5 + 6); // 4
Select 1 correct option.
a 56, 56, 11, 11
b 11, 56, 11, 11
c 56, 56, 56, 11
d 56, 56, 56, 56
e 56, 56, 11, 56
Ans: a
22. Question 21
Which of the following is not a primitive data value in Java?
Select 2 correct options
a "x"
b 'x'
c 10.2F
d Object
e false
Ans: a,d
23. Question 22
What does the zeroth element of the string array passed to the standard main
method contain?
Select 1 correct option.
a The name of the class.
b The string "java".
c The number of arguments.
d The first argument of the argument list, if present.
e None of the above.
Ans: d
24. Question 23
What will the following program print?
public class TestClass
{
static boolean b;
static int[] ia = new int[1];
static char ch;
static boolean[] ba = new boolean[1];
public static void main(String args[]) throws Exception
{
boolean x = false;
if( b )
{
x = ( ch == ia[ch]);
}
else x = ( ba[ch] = b );
System.out.println(x+" "+ba[ch]);
}
}
Select 1 correct option.
a true true
b true false
c false true
d false false
e It'll not compile.
Ans: d
25. Question 24
What will be the result of attempting to compile and run the following code?
public class InitClass
{
public static void main(String args[ ] )
{
InitClass obj = new InitClass(5);
}
int m;
static int i1 = 5;
static int i2 ;
int j = 100;
int x;
public InitClass(int m)
{
System.out.println(i1 + " " + i2 + " " + x + " " + j + " " + m);
}
{ j = 30; i2 = 40; } // Instance Initializer
static { i1++; } // Static Initializer
}
Select 1 correct option.
a The code will fail to compile, since the instance initializer tries to assign a value to a static member.
b The code will fail to compile, since the member variable x will be uninitialized when it is used.
c The code will compile without error and will print 6, 40, 0, 30, 5 when run.
d The code will compile without error and will print 5, 0, 0, 100, 5 when run.
e The code will compile without error and will print 5, 40, 0, 30, 0 when run.
Ans: c
26. Question 25
Which code fragments will print the last argument given on the command line to the standard output, and exit without any output and
exceptions if no arguments are given?
1.
public static void main(String args[ ])
{
if (args.length != 0) System.out.println(args[args.length-1]);
}
2.
public static void main(String args[ ])
{
try { System.out.println(args[args.length-1]); }
catch (ArrayIndexOutOfBoundsException e) { }
}
3.
public static void main(String args[ ])
{
int i = args.length;
if (i != 0) System.out.println(args[i-1]);
}
4.
public static void main(String args[ ])
{
int i = args.length-1;
if (i > 0) System.out.println(args[i]);
}
5.
public static void main(String args[ ])
{
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
}
Select 3 correct options
a Code No. 1
b Code No. 2
c Code No. 3
d Code No. 4
e Code No. 5
Ans: a,b,c
27. Question 26
Consider the following class:
public class ArgsPrinter
{
public static void main(String args)
{
for(int i=0; i<3; i++)
{
System.out.println(args);
}
}
}
What will be printed when the above class is run using the following command line:
java ArgsPrinter 1 2 3 4
Select 1 correct option.
a123
b ArgsPrinter 1 2
c java ArgsPrinter 1 2
d111
e None of these.
Ans: e
28. Question 27
Consider the following class definition:
public class TestClass
{
public static void main(){ new TestClass().sayHello(); } //1
public static void sayHello(){ System.out.println("Static Hello World"); } //2
public void sayHello() { System.out.println("Hello World "); } //3
}
What will be the result of compiling and running the class?
Select 1 correct option.
a It will print 'Hello World'.
b It will print 'Static Hello World'.
c Compilation error at line 2.
d Compilation error at line 3.
e Runtime Error.
Ans: b
29. Question 28
What will the following program print?
public class TestClass
{
static int someInt = 10;
public static void changeIt(int a)
{
a = 20;
}
public static void main(String[] args)
{
changeIt(someInt);
System.out.println(someInt);
}
}
Select 1 correct option.
a 10
b 20
c It will not compile.
d It will throw an exception at runtime.
e None of the above.
Ans: a
30. Question 29
What is the numerical range of short data type?
Select 1 correct option.
a It depends on the platform the JVM is running.
b 0 to 65535
c -32768 to 32767
d 0 to 32767
e -16384 to 16383
Ans: c