Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Oopc Ii Paper

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Section A

1. What is the correct answer when you compile and run the following program?

class A {
static int x;
A(){
System.out.println("Constructor"); }
static { x = 7; }
{ x = 8; }
public static void main(String []args){
System.out.print(x);
A a = new A();
System.out.print(x); }}

A. 7
B. 7Constructor8
C. 87
D. Compile error.

2. What is the correct answer when you compile and run the following program?

class EnumExample2{
enum Season { WINTER, SPRING, SUMMER, FALL; }
public static void main(String[] args) {
Season s = Season.WINTER;
System.out.println(s);
}}
A. WINTER
B. Runtime Exception
C. Print Memory location
D. Compilation fails

3. What will be the output when you compile and run the following Java code?
class Demo {
static void varMeth(int... z){
System.out.println("Var arg method");
}
static void varMeth(int x){
System.out.println("int method");
}
static void varMeth(Integer y){
System.out.println("Integer method");
}
public static void main(String[]args){
varMeth(10);
}
}
A. Var arg method
B. int method
C. Integer method
D. Compilation fails
4. What is the correct answer when you compile and run the program?
class Exc3 {
public static void main(String args[]){
String name="123456789V";
System.out.print("before,");
try{
char ch=name.charAt(10);
}catch(Exception e){
System.out.print("Runtime Error,” );
}
System.out.print("after,");
}
}
A. before, after
B. before, Runtime Error
C. before, Runtime Error, after
D. Compilation fails
E. Runtime Exception

5. What is the correct answer you receive when you compile and run the program?

class A1{
public static void main(String []args)throws Exception{
System.out.print("B4");
throw new Exception();
System.out.println("After");
}
}
A. B4
B. B4After
C. After
D. Compilation fails .
E. Runtime Exception

6. What will be the output when you compile and run the following Java code?
class E1 extends Exception{}
class E2 extends E1{}
class A{
void m() throws E1{
System.out.println("OK-A");
} }
class B extends A{
protected void m() throws Exception{
System.out.println("OK-B");
}
public static void main(String[] args)ex {
A a1=new A();
a1.m();
}}
A. OK-A B. OK-B C.No answer D. Compilation fails . E.Runtime Exception

7. What will be the output when you compile and run the following code?

class Demo{
public static void main(String args[]){
int x=0,d=0;
try{
d=42/0;
}catch(ArithmeticException e){
System.out.print("Arithmetic error..");
}catch(ArrayIndexOutOfBoundsException e){
System.out.print("Array Index error..");
}finally{
System.out.print("finally..");
}
System.out.print("after..");
}
}

A. Arithmetic error.. after..


B. finally.. after..
C. Arithmetic error.. finally.. after..
D. Compilation fails
E. Runtime Exception

8. Which of the following are command line switches used to disable assertions in non-system classes?
A. –da
B. –ad
C. –disableassertions
D. None of the above

9. The following two statements illustrate the difference between


int x = 25;
Integer y = new Integer(25);

A) Primitive data types


B) Primitive data type and an object of a wrapper class
C) Wrapper class
D) None of the above
10. Analyse the following code segment and select the correct answer.
class Demo{
public static void main (String[] args) {
Integer iob=new Integer(100);
String s="0123";
int x=Integer.parseInt(s);
System.out.println(x);
}
}
A. 0123 B. 123 C.83 D. Compilation fails E. Runtime Exception

11. What will be the output of the following program?


class Demo{
public static void main (String[] args) {
Integer x=525;
Integer y=x;
System.out.print(y==x);
y++;
System.out.print(x + " " + y);
System.out.println(y==x);
}
}

A. true525 526false B. true526 526true C. true525 525true D. Runtime


Exception E. Compile error.

12. .​ What will be the output of the program given below?

class MyThread extends Thread {


MyThread(){
System.out.print(" MyThread");
}
public void run(){
System.out.print(" bar");
}
public void run(String s){
System.out.println(" baz");
}
}
class TestThreads{
public static void main (String [] args){
Thread t = new MyThread(){
public void run(){
System.out.println(" foo");
}
};
t.start();
}
}

A. foo B. MyThread foo C. MyThread bar D. foo bar


13. Study the following program and determine its output.
class MyThread extends Thread {
public static void main(String [] args) {
MyThread t = new MyThread();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run() {
System.out.print("Thread ");
}
}

A. Compilation fails
B. An exception occurs at runtime.
C. It prints "Thread one. Thread two."
D. The output cannot be determined
14. What is the output of this program?

class Newthread extends Thread {


Thread t;
Newthread() {
t = new Thread(this,"New Thread");
t.start();
}
public void run() {
System.out.println(t.isAlive());
}
}
class multi {
public static void main(String args[]) {
new Newthread();
}
}

a) 0 b) 1 c) true d) false e)Runtime Exception f).Compile error

15. . What will be the output of the following program?


class s implements Runnable {
int x, y;
public void run() {
for(int i = 0; i < 1000; i++)
synchronized(this) {
x = 12;
y = 12;
}
System.out.print(x + " " + y + " ");
}
public static void main(String args[]){
s run = new s();
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t1.start();
t2.start();
}
}

A. DeadLock B. It prints 12 12 12 12 C. Compilation Error D. Cannot


determine output.

16. . What will be the output of the program?


public class T1 {
private int count = 1;
public synchronized void doSomething()
{
for (int i = 0; i < 10; i++)
System.out.println(count++);
}
public static void main(String[] args)
{
T1 demo = new T1();
Thread a1 = new A(demo);
Thread a2 = new A(demo);
a1.start();
a2.start();
}
}
class A extends Thread {
T1 demo;
public A(T1 td) {
demo = td;
}
public void run(){
demo.doSomething();
}
}

A. It will print the numbers 0 to 19 sequentially


B. It will print the numbers 1 to 20 sequentially
C. It will print the numbers 1 to 20, but the order cannot be determined
D. The code will not compile

17. What will be the output of the program?


public class Test {
public static void main (String [] args) {
final Foo f = new Foo();
Thread t = new Thread(new Runnable() {
public void run(){
f.doStuff();
}
});
Thread g = new Thread(){
public void run(){
f.doStuff();
}
};
t.start();
g.start();
}
}
class Foo{
int x = 5;
public void doStuff(){
if (x < 10){
try{
wait();
} catch(InterruptedException ex) { }
}
else{
System.out.println("x is " + x++);
if (x >= 10){
notify();
}
}
}
}
A. The code will not compile because of an error on notify(); of class Foo.
B. The code will not compile because of some other error in class Test.
C. An exception occurs at runtime.
D. It prints "x is 5 x is 6".

18. . Which two statements out of the following are true?


1.)Deadlock will not occur if wait()/notify() is used
2.)A thread will resume execution as soon as its sleep duration expires.
3.)Synchronization can prevent two objects from being accessed by the same thread.
4.)The wait() method is overloaded to accept a duration.
5.)The notify() method is overloaded to accept a duration.
6).Both wait() and notify() must be called from a synchronized context.

A.1 and 2B.3 and 5 C.4 and 6 D.1 and 3

19. What will output when you compile and run the Java code specified below?

class Demo{
public static void main (String[] args) {
String s=Integer.toString(100,2);
System.out.println(s);}}

A. 100 B. 200 C. 1100100 D. Compiler error

20. Get the output.


class Demo{
public static void main (String[] args) {
String s1=new String("abc");
System.out.print(s1);
s1.concat("def");
System.out.print(s1);}}
21. Study the following program and identify its output.
class Demo{
public static void main (String[] args) {
String s1="abc";
A. String Abcabcdef B. def C. abcdef D. abcabc E. Compiler error
s2="a";
String s3=s2+"bc";
System.out.println(s1==s3);}}

A. abc==abcB .true C. false D. abcabc E. Compiler error

22. What will be the output of the program?

class Demo {
public static void main(String[] args) {
String s1 = new String("abcdef");
charch = s1.charAt(6);
System.out.println("index 6:" + ch);}}

A. index 6:f B. index 6:null C. An exception is thrown at runtime D. Compiler error

23. Which of these process occur automatically by the Java run time system?
a) Serialization
b) Garbage collection
c) File Filtering
d) All of the mentioned

24. Identify and select the output of the following program.

import java.io.*;
class files {
public static void main(String args[]) {
File obj = new File("/ocjp/system");
System.out.print(obj.getName());
}
}
a) ocjp
b) system
c) ocjp/system
d) /ocjp/system

25. What is the output of this program?


import java.io.*;
class serialization {
public static void main(String[] args) {
try {
Myclass object1 = new Myclass("Hello", -7, 2.1e10);
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
int x;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
x = ois.readInt();
ois.close();
System.out.println(x);
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
} } }
class Myclass implements Serializable {
String s;
int i;
double d;
Myclass(String s, int i, double d){
this.d = d;
this.i = i;
this.s = s;
}
}

a) -7
b) Hello
c) 2.1E10
d) deserialization

26. . Which of these is an interface in Java for controlling serialization and deserialization?

a) Serializable
b) Externalization
c) FileFilter
d) ObjectInput

27. Identify and choose the output of the following program.

import java.text.*;
import java.util.*;
class Date_formatting {
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("mm:hh:ss");
System.out.print(sdf.format(date));
}
}
Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hour time).

a) 3:55:4
b) 3.55.4
c) 55:03:04
d) 03:55:04

28. What is the output of this program?


import java.text.*;
import java.util.*;
class Date_formatting {
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("E MMM dd yyyy");
System.out.print(sdf.format(date));
}
}

Note: The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time).

a) Mon Jul 15 2013


b) Jul 15 2013
c) 55:03:04 Mon Jul 15 2013
d) 03:55:04 Jul 15 2013

29. Read and comprehend the following code in order to identify its output.
public static void main(String[] args) {
Object obj = new Object() {
public int hashCode() {
return 42;
}
};
System.out.println(obj.hashCode());
}

A. 42
B. Runtime Exception
C. Compile Error at line 2
D. Compile Error at line 5

30.
x = 0;
if (x1.hashCode() != x2.hashCode() ) x = x + 1;
if (x3.equals(x4) ) x = x + 10;
if (!x5.equals(x6) ) x = x + 100;
if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
System.out.println("x = " + x);

and assuming that the equals() and hashCode() methods are property implemented,
if the output is "x = 1111", which of the following statements will always be true?
A. x2.equals(x1)
B. x3.hashCode() == x4.hashCode()
C. x5.hashCode() != x6.hashCode()
D. x8.equals(x7)

31. What will be the output of the following Java program?


import java.util.*;
class vector {
public static void main(String args[]) {
Vector obj = new Vector(4,2);
obj.addElement(new Integer(3));
obj.addElement(new Integer(2));
obj.addElement(new Integer(5));
obj.removeAll(obj);
System.out.println(obj.isEmpty());
}
}

a) 0
b) 1
c) true
d) false
32. Select the output of this program?
import java.util.*;
class Output {
public static void main(String args[]) {
TreeSet t = new TreeSet();
t.add("3");
t.add("9");
t.add("1");
t.add("4");
t.add("8");
System.out.println(t);
}
}
a) [1, 3, 5, 8, 9]
b) [3, 4, 1, 8, 9]
c) [9, 8, 4, 3, 1]
d) [1, 3, 4, 8, 9]

33. . What is the output of this program?

import java.util.*;
class Maps {
public static void main(String args[]) {
TreeMap obj = new TreeMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj.entrySet());
}
}
a) [A, B, C]
b) [1, 2, 3]
c) {A=1, B=2, C=3}
d) [A=1, B=2, C=3]

34. . Analyse the following piece of code and select the resulting output?

import java.util.*;
class Linkedlist {
public static void main(String args[]) {
LinkedList obj = new LinkedList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.removeFirst();
System.out.println(obj);
}
}

a) [A, B]
b) [B, C]
c) [A, B, C, D]
d) [A, B, C]
35. Identify and select the output of the following program.

import java.util.LinkedList;
import java.util.Queue;

class M {
public void queueExample() {
Queue queue = new LinkedList();
queue.add("Java");
queue.add("DotNet");
queue.offer("PHP");
queue.offer("HTML");
System.out.print(queue.remove());
System.out.print( queue.poll());
System.out.println( queue.peek());
}
public static void main(String[] args) {
new M().queueExample();
}
}

a) PHPDotNet
b) JavaDotNetPHP
c) Runtime Exception
d) Compiler error
36. What is the output of this program?

import java.util.*;
class Demo{
public static void main(String args[]){
ArrayList<A>ar=new ArrayList<A>();
ar.add(new A(34));
ar.add(new A(74));
ar.add(new A(64));
ar.add(new A(44));
ar.add(new A(54));
ar.add(new A(24));
Collections.sort(ar,new MyComp());
System.out.println(ar);
}
}
class A {
int a;
A(int i){a=i;}
public String toString(){
return Integer.toString(a);
}
}
class MyComp implements Comparator<A>{
public int compare(A r1, A r2){
if(r1.a>r2.a) return 1;
else if(r1.a<r2.a) return -1;
else return 0;
}
}

a) [24, 34, 44, 54, 64, 74]


b) [34, 74, 64, 44, 54, 24]
c) Runtime Exception
d) Compiler error

37. What is the output of this program?

import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5 - i] = i;
Arrays.sort(array);
for (int i = 0; i < 5; ++i)
System.out.print(array[i]);;
} }

a) 12345
b) 54321
c) 1234
d) 5432
38. What is the correct answer you receive when you compile and run the program?
import java.util.*;
class Demo{
public static void main(String args[]){
ArrayList<A>ar=new ArrayList<A>();
ar.add(new A(200));
ar.add(new A(500));
ar.add(new A(300));
ar.add(new A(150));
System.out.println(ar);
Collections.sort(ar);
}
}
class A implements Comparable<A>{
int a;
A(int i){a=i;}
public String toString(){
return Integer.toString(a);
}
public int compareTo(A r){
if(a>r.a)
return 1;
else if(a<r.a)
return -1;
else
return 0;
}
}

a) [200, 500, 300, 150]


b) [150, 200, 300, 500]
c) Runtime Exception
d) Compiler error
39. . What is the output of this program?

import java.util.*;
class Demo{
public static void main(String args[]){
ArrayList<String>ar=new ArrayList<String>();
ar.add(new String("D"));
ar.add(new String("E"));
ar.add(new String("F"));
ar.add(new String("A"));
ar.add(new String("C"));
ar.add(new String("B"));

System.out.println(ar);

Collections.sort(ar);
System.out.println(ar);
}
}
a) [D, E, F, A, C, B]
b) [A, B, C, D, E, F]
c) Runtime Exception
d) Compiler error
40. QUESTION
import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5 - i] = i;
Arrays.sort(array);
System.out.print(Arrays.binarySearch(array, 4));
}
}
a) 2
b) 3
c) 4
d) 5

41. What is the correct answer you receive when you compile and run the following program?

import java.util.*;
class Output {
public static double sumOfList(List<? extends Number> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
public static void main(String args[]) {
List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
System.out.println(sumOfList(ld));
}
}

a) 5.0
b) 7.0
c) 8.0
d) 6.0

42. . What is the output of this program?

import java.util.*;
class Output {
public static void addNumbers(List<? super Integer> list) {
for (int i = 1; i <= 10; i++) {
list.add(i);
}
}
public static void main(String args[]) {
List<Double> ld = Arrays.asList();
addnumbers(10.4);
System.out.println("getList(2)");
}
}
a) 1
b) 2
c) 3
d) Compilation fail

43.
class D {
public static void main (String args[]) {
Byte a = new Byte("1");
byte b = a.byteValue();
short c = a.shortValue();
char d = a.charValue();
int e = a.intValue();
long f = a.longValue();
float g = a.floatValue();
double h = a.doubleValue();
System.out.print(b+c+d+e+f+g+h);
}
}
What is the result of attempting to compile and run the program?

A. Prints: 7
B. Prints: 7.0
C. Compiler Error
D. Runtime Error
E. None of the Above

44. QUESTION
class Outer {
class Inner{ }
}
class Test{
public static void main (String [] args) {
Outer f = new Outer();
/* Line 07: Missing statement ? */
}
}

which statement, inserted at line 07, creates an instance of Inner?


A.Outer.Inner b = new Outer.Inner();
B.Outer.Inner b = f.new Inner();
C.Inner b = new f.Inner();
D.Inner b = f.new Inner();
45. What is the output of this program?
class Outer{
void mOuter(){
int x=10;
class Local{
void mLocal(){
System.out.println("X:"+x);
}
}
Local ob=new Local();
ob.mLocal();
}
}
class DemoLocal{
public static void main(String args[]){
Outer ob=new Outer();
ob.mOuter();
}
}

A.X10
B. X0
C. An exception is thrown at runtime
D. Compiler error

46. What will be the output of the following Java program?


class A{
void m(){
System.out.println("A.m()");
}
}
class B{
A a1=new A(){
void m(){
System.out.println("Anonymous.m()");
}
};
}
class Demo{
public static void main(String args[]){
B b1=new B();
b1.a1.m();
}
}

A. A.m()
B. Anonymous.m()
C. An exception is thrown at runtime
D. Compiler error
47. What will be the output when you compile and run the following Java code?
class A{
int i=100;
static int i1=200;
static class Inner{
static int j=10;
static void mn(){
System.out.print(i);
System.out.print(j);
System.out.print(i1);
}
}
}
class DemoA{
public static void main(String args[]){
A.Inner.mn();
System.out.println("Inner :"+A.Inner.j);
}
}

A. 10010200 B. 100100
C. An exception is thrown at runtime
D. Compiler error
48. JAR file contains the compressed version of
a).java file
b).class file
C).jsp file
d).None of above

49. What is the output of this program?

import static java.lang.Integer.*;


import static java.lang.Byte.*;
class A {

public static void main(String args []) {


System.out.println(MAX_VALUE);
}
}

a).2147483647
b).127
c). None of above
d). Compiler error

50. Which of these methods is given a parameter via command line arguments?
a) main().
b) recursive() method.
c) Any method.
d) System defined methods.d) Only ASCII characters can be converted.
Section B - ​Structured Essay Questions

1. What differences exist between HashMap and HashTable?

2. What value does readLine(), read() return when it has reached the end of a file?

3. How do you ensure that N threads can access N resources without deadlock?

4. What is the difference between a synchronized method and a synchronized block?

5. What is an IO stream?

Section A

stion No ct Answer Answer Description

Static variables and static blocks load to the memory when the program loads. Therefore variable x is loaded
and value 7 is initialized with the loading process of the program. When the main method is executed, it
prints the value of x, which is 7. Subsequently, when an object from class A is created it executes the
1 (b) instance block and accordingly assigns 8 to variable x. This results in the constructor body being executed
and then printing “Constructor” as the output.The main method prints the value of x, which is 8, as the final
output of the program.

an be declared inside a class. There are four constant class bodies for Season enum. When printing the object of
2 (a)
enum, it will always prints its name.

There are 3 varMeth methods in Demo class. There is no compile error because these methods have the same
method but different parameter lists and this is known as “overloading”. When calling the method varMeth,
only a single method will execute and it will depend on the value of the argument. The main method calls
3 (b)
the method varMeth by passing primitive int 10 and the program includes a method with a compatible
parameter list that can acquire the primitive int.

gram compiles and runs without producing any errors. There is a String variable “name” and it has a word
with 10 characters. Each and every character of this variable has an index number. This particular index
starts with 0 for letter ”1” and ends with 9 for letter “v”. In this program it takesthe 10​th​character from this
variable using charAt method. As there is no character for index 10, it will cause a
4 (c)
StringIndexOutofBoundsException, and the catch statement will catch the exception that is thrown as a
result. Consequently, the program does not stop execution but the
executes the body of the catch statement.

The program fails compilation as the “throw” keyword definitely throws an Exception. The program lines
5 (d) after the “throw new Exception();” line will never be executed. It is pointless to include code that will never
be executed. A program generally cannot have unreachable statements.

The program fails to compile and there is an overriding rule that states that it is impossible to override a
method with a wider checked exception. Class B is attempting to override m() method but it throws a wider
6 (d)
exception type than the m method in class A. Furthermore, it should be noted that E1 is a subtype of
Exception.

There are no compile and runtime errors in this program. This program is attempting to divide 42 by 0 and
this results in an ArithmeticException. After throwing an exception it stops the execution of the try block and
7 (c)
first checks the catch block and sees whether it can catch the exception or not. The catch block will be able to
catch the exception and it prints “Arithmetic error”. Therefore, the second catch does not execute but the
finally statement executes and prints “finally”. Subsequently, the program continues and prints “after”.
Two command-line switches used to disable assertions are -da and -disableassertions. To disable assertions
8 and c)
in system classes, a different switch is used. It is important to note that all of the letters are in lowercase.

9 (b) Variable x is declared with a primitive data type int but y is declared with the type Integer. Integer is a
wrapper class and an object has been declared from the Integer class.

The parseInt method of the Integer class for converting String to the primitive int type can be used .however,
10 (b) the parseInt method is unable to convert Octal to Decimal and therefore the program prints the value of x as
123.

Variable x’s value is assigned to variable y. Therefore, the first print method prints true because both x and y
refer to the same object. Wrapper objects are immutable therefore the increment makes a new wrapper object
11 (a)
for y. Consequently, the second print method prints the value of x as 525 and the value of y as 526. The third
print method prints false because x and y refer to two different objects.

The program compiles and runs without errors. The main method declares an anonymous inner class that
12 (b) extends the MyThread class and overrides the run method. Therefore the constructor of the parent class is
executed and prints “MyThread". Subsequently, the newly created thread is initiated and its run method
prints “foo”.

This program compiles without producing errors but provides an exception while running. It is not possible
13 (b) to start the thread more than once and this results in the program providing an exception when calling the
start method on the second time.

The main method creates a new Thread object from the NewThread class because the NewThread class
extends the Thread class. After creating an object, the constructor also starts to execute. A new Thread is
created in this constructor by passing the current object (NewThread object) and the String as the thread
14 (c) name. Afterwards, the thread is started and it executes the run method of the runnable object that is passed to
the constructor by the “this” keyword. The “this” keyword representsthe current object and the current object
is from the NewThread class. The run method of the NewThread class prints true because t is the currently
running thread and it is alive. Furthermore, the NewThread is a runnable class because NewThread extends
the Thread class and the Thread class implements the Runnable interface.

Two Threads have been created and started by passing a single runnable object. The program cannot be stuck
as a Deadlock as the run method has only one synchronize block. Both threads run perfectly and do not
15 (b)
change the x and y variables within the for loop. Afterwards, “12 12”is printed from one thread and “12 12”
is again printed from the other thread.

Two treads have been created and started by passing an object from the T1 class. Thread scheduler can
choose one thread to execute. When the thread starts to execute, it calls the doSomething method and it is
16 (b) synchronized. Therefore,the object from T1 class is locked and prints from 1 to 10 by incrementing the count
variable. Subsequently, the second thread can execute the doSomething method by locking the same object
and it prints from 11 to 20 by incrementing the count variable.

The program compiles without producing any errors but produces an exception at runtime. This exception is
17 (c) created due to the fact that wait and notify methods should be called within a synchronized method or a
synchronized block.

1 is incorrect because Deadlock can occur while using wait and notify. 2 is incorrect as the Thread is directed
18 (c) to a ready state after sleep. Therefore, it has to wait until another thread is still running. 3 is incorrect because
if there is only one thread program, it does not need synchronization. 5 is also incorrect as the notify method
does not accept duration and it does not get overloaded.

The toString method of the Integer class converts primitive int to string and by giving radix it converts the
19 (c) decimal int value to the given redix and then converts to String. In this question, 100 converts to radix 2 and
then converts to string. Therefore it prints 1100100.

20 (d) The first print statement prints the value of s1 as “abc” and then concatenates “def” to s1 but String objects
are immutable. Therefore, the value of s1 does not change and the second print statement prints “abc” again.

21 (c) The new String object is created by concatenating the value of s2 and “bc”. Therefore s1 and s3 have two
different objects. It prints false by checking equality on s1 and s3.

There is a String variable “s1” and it has a word with 6 characters. Each and every character of this variable
22 (c) has an index number. The index starts with 0 for letter “a” and ends with 5 for letter “f”. In this program it
will take the 6​th​ character from this variable using the charAt method. However, there is no character for
index 6 and this will cause a StringIndexOutofBoundsException exception.
Garbage Collection is a program run by the Java runtime system. The Garbage Collector cannot be forced
23 (b)
and it destroys objects that the programs does not use.

The File object requires a name and the getName method of the class File gives only the name of the
24 (b)
file.Therefore, it returns system as output and ocjp is the path.

Program serializes the MyClass object to the serial file and it operates successfully. However, it deserializes
25 (d) int using the readInt method and as an int to be deserialized is not available, the EOFException exception is
thrown. A catch statement is present to catch the exception and “deserialization” is printed as the output.

​It is only possible to serialize and deserialize objects that have been instantiated from the class that
26 (a)
implements the Serializable interface.

The SimpleDateFormat class is used to format the date and time according to a given pattern. According to
the given program, the time format is “mm:hh:ss” and this means that the minutes are specified first, next the
27 (c)
hours, and finally the seconds. When the format method of the SimpleDateFormat class is executed, the
output will be “55:03:04”.

The SimpleDateFormat class is used to format the date and time according to a given pattern. In the given
program, the time format is “E MMM dd yyyy” and this means that the day of the week is specified first,
28 (a)
next the month, the date, and finally the year. When the format method of the SimpleDateFormat class is
executed, the output is given as “Mon Jul 15 2013”.

The program compiles and runs without producing errors. The anonymous inner class that extends the Object
29 (a) class has created and has overridden the hashCode method. Consequently, this particular program prints 42
as the output.

30 (b) If x3 meaningfully equals to x4,the return values of the hashCode methods should be the same.

31 (c) The addElement method of the vector class adds elements to the vector. The removeAll method removes
vector elements. The isEmpty method returns true because the vector is empty.

32 (d) TreeSet can store objects and the TreeSet sorts the added objects despite the insertion order.

The program adds data to the TreeMap object using the put method. Data should be stored as a key and a
33 (d) value pair. The entrySet method returns a set view of the mappings contained in this map and these are sorted
in the ascending key order.

34 (b) Data can be stored in LinkedList using the add method. The RemoveFirst method of LinkedList removes the
first element of the list and the output is printed as [B,C].

The add and offer methods insert data to Queue. The first print method prints “Java” as the output and the
35 (b) remove method removes the first value. The second print statement prints “DotNet” as output and the poll
method also removes the first value. The last print statement prints “PHP” as the output and the peek method
does not remove the value.

The collections.sort method can sort ArrayList. But the sort method cannot sort the object from class A
36 (b) because the sort method does not know the structure of class A. Therefore the MyComputer class
implements the Comparator interface and overrides the Compare method to describe the sorting algorithm.
Next, the sort method compares the object of class A and places them in the correct order.

There is an empty array with a size of 5. The first for loop inserts the numbers starting 5 to 1. Next, the
37 (a) Arrays.sort method sorts the array of ints in the ascending order. The second for loop prints the values in the
sorted array.

The ArrayList have added the object from class A and printed the arrayList and this provides the output [200,
38 (a) 500, 300, 150]. Next, the arrayList is sorted using the Collections.sort method and class A implements
comparable interfaces and overrides the CompareTo method by describing the sorting algorithm.

The String objects are added to the ArrayList and the ArrayList is printed. The result is the output [D, E, F,
39 a,b) A, C, B] and then the ArrayList is sorted. The sort method sorts the specified list into the ascending order
and then prints the ArrayList. Accordingly, it prints [A, B, C, D, E, F] as the output.

There is an empty array with a size of 5. The for loop inserts the numbers starting from 5 to 1. Next the
40 (b) Arrays.sort method sorts the array of ints into the ascending order. The Arrays.binarySearch method searches
the number 4 in the array and it returns the index of the number 4 as “3”.
The Arrays.asList method returns a fixed-size list backed by the specified array. The sumOfList method
41 (b) receives a List with a generic type that extends the Number class. The Id list is able to pass to the method as
the list has a generic type of Double. Next, the numbers in the list are calculated and the total is returned.

Compilation fails because the addNumbers method is able to take a list with a generic type that extends the
42 (d) Integer. However, when calling the method, it passes a list with the generic type of Double and Double is not
a subclass of Integer.

A compiler error is generated because the Byte class does not have a charValue method. The Byte class
extends the Number class and implements all six of the Number methods. It is interesting to note that four of
43 (c) the Number methods are abstract while two, byteValue and shortValue, are not abstract. This is due to the
fact that there is a concrete implementation of the byteValue and shortValue that calls the intValue method
and then casts the result to the appropriate type.

44 (b) Inner is a regular inner class and therefore the Outer class instance should be created prior to the creation of
the Inner class instance. Only the B output makes the Inner class object using the Outer class object.

45 (d) The method local inner class can only access final members from within the method. Therefore compilation
fails when attempting to print the value of the variable x.

46 (b) The anonymous Inner class that extends A has being declared in class B and it overrides method m. The
main method executes the overridden m method of anonymous inner class according to polymorphism.

47 (b) Fails to compile because the static nested Inner class can be accessed only by the static members from the
Outer class. But the mn method attempts to access variable I from the Outer class.

48 (b) Jar file can store collection of class files.

49 (d) The reference to MAX_VALUE is ambiguous, both variable MAX_VALUE in Integer and variable
MAX_VALUE in Byte match.

50 (a) When a class is executed, argument values can be passed to the parameter list of the main method.

Section B
1. Answer
● Hashtable​ is synchronized, whereas ​HashMap​ is not. This makes HashMap better for non-threaded applications, as
unsynchronized Objects typically perform better than synchronized ones.
● Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

1. Answer
● It returns null because BufferedReader object reading the data as string from the file, at the end if the content is not available
it returns null.

2. Answer
There are two ways to create a new thread.
a) Extend the Thread class and override the run() method in your class. Create an instance of the subclass and invoke the start()
method on it, which will create a new thread of execution. e.g.

public class NewThread extends Thread{

public void run(){


// the code that has to be executed in a separate new thread goes here
}
public static void main(String [] args){
NewThread c = new NewThread();
c.start();
}}
b) Implements the Runnable interface. The class will have to implement the run() method in the Runnable interface. Create an
instance of this class. Pass the reference of this instance to the Thread constructor a new thread of execution will be created.
e.g. class

public class NewThread implements Runnable{

public void run(){


// the code that has to be executed in a separate new thread goes here
}
public static void main(String [] args){
NewThread c = new NewThread();
Thread t = new Thread(c);
t.start();
}}

3. Answer

● The synchronization is the capability to control the access of multiple threads to shared resources. Without
synchronization, it is possible for one thread to modify a shared resource while another thread is in the process of using or
updating that resource.
● There two synchronization syntax in Java Language. The practical differences are in controlling scope and the
monitor.
● With a synchronized method, the lock is obtained for the duration of the entire method.
● With synchronized blocks you can specify exactly when the lock is needed.

4. Answer
● An ​I/O Stream​ represents an input source or an output destination. A stream can represent many different kinds of
sources and destinations, including disk files, devices, other programs, and memory arrays.
● Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and
objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.

You might also like