You are on page 1of 5

OCA Practice Test 2 Java 1

Name of the Participant: Date:

Duration: 25 mins Questions : 10


Assume all public classes are defined in separate java files, necessary APIs are imported and pieces of
code are run in main method.
1. What is the output?
public class Test {
public static void main(String[] args) {
List<String> vehicles= new ArrayList<String>();
vehicles.add("car");
vehicles.add("van");
vehicles.add("bus");
vehicles.add("truck");
if(vehicles.remove("bus")) {
vehicles.remove("van");
}
System.out.println(vehicles);
}
}
a) [car, van, bus, truck] b) [car, truck] c) [car, bus, truck] d) throws Exception

2. What is the output?

int i = 0;

int j = 7;

for (i = 0; i < j-1;) {

System.out.print(++i +" "+ --j+"; ");

a) 0 7; 1 6; 2 5; 3 4; b) 1 7; 2 6; 3 5; 4 4;

c) 1 6; 2 5; 3 4; d) throws Exception
OCA Practice Test 2 Java 2

3. What is the output?


public class Test {
public static void main(String[] args) {
Test t =new Test();
t.printVal(23);
}
public void printVal(short val) {
System.out.println("short val = " + val);
}
public void printVal(Long val) {
System.out.println("Long val = " + val);
}
public void printVal(Object val) {
System.out.println("Object val = " + val);
}
}
a) Object val = 23 b) Long val = 23 c) Short val = 23 d) Compile error

4. What is the output?


public class Test {
public static void main(String[] args) {
Book [] books = {
new Book("Java",5),
new Book("C++",8),
new Book("Python",3)
};
System.out.println(books);
System.out.println(books[1]);
System.out.println(books[2].getCopies());
}
}

public class Book {


private String title;
OCA Practice Test 2 Java 3

private int copies;


public Book(String title, int copies) {
this.title = title; this.copies = copies;
}
public String toString()
{ return "Title=" + title + ", Copies=" + copies; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public int getCopies() { return copies; }
public void setCopies(int copies) { this.copies = copies; }
}
a) [LBook;@6d06d69c
Title=Java, Copies=5
8
b) books
Title=Java, Copies=5
8
c) [LBook;@6d06d69c
Book@7852e922
3
d) [LBook;@6d06d69c
Title=C++, Copies=8
3

5. What is output?
public class Test {
static { System.out.print(" static"); }
{ System.out.print(" instance"); }
public Test () {
System.out.print(" constr");
}
public static void main(String args[]) {
System.out.print(" main");
Test t = new Test();
OCA Practice Test 2 Java 4

}
}
a) static instance main constr b) static main instance constr
c) static constr constr main d) Will not compile

6. What is the output?

public class Test extends Parent{


public void calculate() {
System.out.println("in Test");
}
public static void main(String[] args) {
Test p = (Test)new Parent();
p.calculate();
}
}
public class Parent {
protected void calculate() {
System.out.println("in Parent");
}
}
a) Compile error b) in Parent c) in Test d) Throws Exception

7. What is output?
public class Test {
public static void main(String[] args) {
int[] arr = {3,4,5,4,5,6};
for (int i : arr) {
System.out.print(arr[i-1] + " ");
}
}
}
a) Compile error b) 3,4,5,4,5,6 c) 5 4 5 4 5 6 d) ArrayIndexOutOfBoundsException
OCA Practice Test 2 Java 5

8. What is the output?


public class Test {
public void check(String str) {
if(str.equals("java")?false:true) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
public static void main(String[] args) {
new Test().check("java");
}
}
a) NO b) YES c) Compile error d) throws Exception

9. Which are TRUE of an interface? Select all correct statements.

A. Cannot be instantiated

B. Marker interfaces have only one abstract method

C. Implemented methods should be default or static

D. Fields are public, final and static

E. Can extend another interface

10. What is the output?


String[] arr = { "car", null, "van", "bus", null };
List arrL = Arrays.asList(arr);
arrL.stream().forEach(System.out::print);

a) carvanbus b) carnullvanbusnull c) carnullvanbus d) Compile error

You might also like