Java- Aptitude Questions
Basic level of the Java assessment. This is to test your skills in Fundamentals of java, Object Oriented P rogramming, Collections and Generics , etc...
Question Number 1
Which of the following correctly maps the default values of the array elements of the types indicated?
a. String → "null"
b. float → 0.0
c. boolean → true
d. char → '\u0000'
Question Number 2
What will be the output of the following code snippet?
public class LoopTest {
public static void main(String[] args)
{
int[] aLoop = {1,3,5,7,9};
for(int x : aLoop)
{
for(int j = 0; j < 3; j++)
{
if(x > 4 && x < 8) continue;
System.out.print(" " + x);
if(j == 1) break;
continue;
}
continue;
}
}
}
a. 1 1 3 3 9 9
b. 1 3 3 9 9
c. 5 5 7 7
d. 1 3 9
Question Number 3
Select from below options what is the right order in which the compilation units (class, interface, package and import statements) must appear in a file?
a. Package declaration
Import statements
Class, interface definitions
b. Import statements
Package declaration
Class, interface definitions
c. Package declaration
Class, interface definitions
Import statements
d. None of these
Question Number 4
Which of the following statements will NOT cause an early termination of the loop?
a. continue
b. return
c. System.exit()
d. break
Question Number 5
Consider the following lines of code:
byte xValue=64;
byte yValue=5;
byte zValue= (byte)(xValue*yValue);
After execution, what is the value of zValue?
a. 320
b. 0
c. 64
d. 645
Question Number 6
Select the static methods which are defined in the java.lang.String class.
a. valueOf
b. length
c. indexOf
d. compare
Question Number 7
Which of the following code fragments will compile without errors?
a. float f = 56.66;
b. boolean v = false,i;
c. double d = 34.56L;
d. char c = "d";
Question Number 8
Which of the following options correctly places a file named 'Test.java' in a directory 'src/test/'?
a. package src.test.Test.java;
b. package src;
package test;
public class Test
{
}
c. package src.test;
public class Test
{
}
d. package src/test;
public class Test
{
}
Question Number 9
Which of the following is the result of compiling and running the following program?
public class Test
{
public static final String TEST = "test";
public static void main(String[ ] args)
{
Test b = new Test();
Sub s = new Sub();
System.out.print(Test.TEST);
System.out.print(Sub.TEST) ;
System.out.print(b.TEST);
System.out.print(s.TEST);
System.out.print(((Test)s).TEST);
}
}
class Sub extends Test { public static final String TEST= "test1"; }
a. testtest1testtest1test
b. test1test1test1test1test1
c. testtesttesttest1test
d. testtesttesttest1test1
Question Number 10
With respect to the below code choose the correct option.
public class Test
{
private static String var1;
private int var2;
private String var3;
void method1()
{
String var4;
}
void method2()
{
}
void method3()
{
}
}
a. var4 is an instance variable
b. var2 is a class variable
c. var1 is a class variable
d. var3 is a local variable
Question Number 11
What will be the output of the following program?
class Parent
{
private final void callParent()
{
System.out.println("Parent");
}
}
public class Child extends Parent
{
public final void callParent()
{
System.out.println("Child");
}
public static void main(String [] args)
{
new Child ().callParent();
}
}
a. Child
b. Compilation fails
c. Child
d. Parent
Parent
Question Number 12
Consider a method with the below signature.
void test(Double r)
Which of the following invocations will result in a compiler error?
a. test(2)
b. test(.2d)
c. test(2D)
d. test(2.0)
Question Number 13
Which of the following statements is TRUE about the program given below?
interface Test1 { }
interface Test2 extends Test1 { }
class Test3 implements Test2 { }
class Test4 extends Test3 implements Test2 { }
public class Test
{
public static void main(String[] args)
{
System.out.println(new Test4() instanceof Test3); //line 1
System.out.println(new Test4() instanceof Test2);//line 2
System.out.println(new Test4() instanceof Test1);//line 3
System.out.println(new Test3() instanceof Test4);//line 4
System.out.println(new Test3() instanceof Test1);//line 5
}
}
a. All lines will evaluate to true
b. Only line 1 and 2 will evaluate to true
c. All lines will evaluate to true and only line 4 will evaluate to false
d. Lines 3 and 5 will evaluate to false
Question Number 14
Consider the following code segment:
import myLibrary.*;
class TestClass
{
// code for the class...
}
What should be the name of this file in order to compile and run the code segment successfully?
a. TestClass
b. TestClass.java
c. Any filename with the extension .java
d. TestClass.class
Question Number 15
Which of the following keywords cannot be used for an abstract method in abstract class?
a. abstract
b. static
c. public
d. void
Question Number 16
Consider a method with the below signature.
public void testDouble(double a)
Which of the following invocations will result in a compiler error?
a. testDouble(0.5+0.1)
b. testDouble(0.3,0.2)
c. testDouble(0.2f)
d. testDouble(0)
Question Number 17
A __________ is a collection with no duplicate elements.
a. List
b. Set
c. Map
d. Queue
Question Number 18
Which of the following is a Java collection interface?
a. ConcurrentSkipListSet
b. LinkedBlockingDeque
c. ConcurrentSkipListMap
d. NavigableMap
Question Number 19
Which of the following methods is to be used for adding elements to a generic Map?
a. insert()
b. put()
c. addvalue()
d. add()
Question Number 20
What is the legal syntax for accessing collection loop in generics?
a.
void printList(Collection<Object> c)
{
for (Object e =c)
{
System.out.println(e);
}
}
b.
void printList(Collection<Object> c)
{
for (int count=0 ; count < c.size();)
{
System.out.println(e);
}
}
c.
void printList(Collection<Object> c)
{
for (c)
{
System.out.println(e);
}
}
d.
void printList(Collection<Object> c)
{
for (Object e : c)
{
System.out.println(e);
}
}