Oopc Ii Paper 3

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

01.What will happen when you attempt to compile and run this program?

enum Feedback { //Line 1


GOOD, BAD; //Line 2

private Feedback() { //Line 3


System.out.println(1); //Line 4
}
}

class Test {

public static void main(String[] args) {


Feedback s = Feedback.GOOD; //Line 5
}
}

a. Compile error at Line 1


b. Compile error at Line 2
c. Compile error at Line 3
d. Compile error at Line 4
e. Compile error at Line 5
f. 1
g. 1 , 1

02.Which of the following are command line switches used to disable assertions in system classes?

a. -sad
b. -saoff
c. -dsysa
d. -das
e. -dsa
f. -disablesystemassertion
g. -systemassertionsdisable
h. None of the above

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

class A extends Thread {

public void run() {

for (int i = 0; i < 10; i++) {

try {
System.out.println(i++);
Thread.sleep(1000); // Line 1
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {

Thread a = new A(); // Line 2


a.start();

A a1 = (A) a; // Line 3
a1.start();

}
}

a. Compile Error At Line 1


b. Compile Error At Line 2
c. Compile Error At Line 3
d. Runtime Exception
e. None of the above

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

class A {
public static void main(String[] args) {
String s1 = "ABC";
String s = "ABC" + "ABC";
String s2 = "ABCABC";
String s3 = s1 + "ABC";
System.out.print( (s == s2) +” , ” );
System.out.print(s == s3);
}
}

a. true , true
b. true , false
c. false , true
d. false , false
e. compile error

05.What is the result of attempting to compile and run the program?

import java.util.regex.Pattern;

class A{

public static void main(String[] args) {

String txt = "Gayan : 0771234567";


String r = "[0-9]{10}"; //Line 1

Pattern p = Pattern.compile(r); //Line 2


System.out.println(p.matcher(txt).find()); //Line 3
}
}

a. Compile error at Line 1


b. Compile error at Line 2
c. Compile error at Line 3
d. false
e. true
f. Runtime Exception

06.Which interface or class of the java.util package offers the FIFO behavior?

a. List
b. Map
c. Set
d. Queue
e. Stack
f. Vector
07.What is the correct answer you receive when you compile and run the following program?

import java.util.*;

class DVD {

String name;

DVD(String name) {
this.name = name;
}

public static void main(String[] args) {


TreeSet ss = new TreeSet();
ss.add(new DVD("Mr. Bean"));
ss.add(new DVD("Ben 10"));
ss.add(new DVD("Scooby Doo"));
ss.add(new DVD("Tom & Jerry"));

System.out.println(ss);
}
}

a. [Ben 10 , Mr. Bean , Scooby Doo , Tom & Jerry]


b. [Tom & Jerry , Scooby Doo , Mr. Bean , Ben 10]
c. Compile Error
d. Print Memory Locations
e. Runtime Exception

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

class Demo {

public static void main(String[] args) {


try {
int x = 10 / 0; // Line 1
} catch (Exception e) { // Line 2
System.out.println("Exception");
} catch (ArithmeticException e) { // Line 3
System.out.println("Arithmetic Exception");
}
}
}

a. Arithmetic Exception
b. Exception
c. Compile Error At Line 1
d. Compile Error At Line 2
e. Compile Error At Line 3

09.class A{
....... class B{
}
}

Which modifiers suit for the blank?

a. final
b. synchronized
c. static
d. native
e. private
10. What is the output of this program?

class A {

public static void main(String[] args) {


Boolean a = true;
Boolean b = true;
boolean x = true;
boolean y = true;
System.out.println(a == b);
System.out.println(x == y);
}
}

a. true , true
b. true , false
c. false , true
d. Throw an Exception
e. Compile Error

11.What is the method, used to add values into TreeMap?

a. put(Object value)
b. put(Object key, Object value)
c. add(Object value)
d. add(Object key, Object value)
e. set(Object key, Object value)

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

import java.io.*;

class A {

public static void main(String[] args) {


File f = new File("abc.txt");
boolean b = f.createNewFile();
System.out.println(b);
}
}

1.Compile Error
2.Create a text file in C:\ partition
3.Create a text file at Class File location
4.throw an Exception
5.Create a text file and can’t determine the location

13.Select the output of this program?

class A {

public static void main(String[] args) {


try {
throw new RuntimeException();
} finally {
System.out.println("final");
}
}
}

a. final
b. Compile Error
c. throw RuntimeException
d. throw RuntimeException , final
e. final , throw RuntimeException
14. What is the correct compiling statement to place A.class file in Classes package.(Note that A.java file contains only class
A{})

1.cd MyApp
javac -d MyApp/Classes MyApp/Source/A.java

2.cd MyApp
javac -d -change Classes Source/A.java

3.cd MyApp
javac -d Classes Source/A.java

4.cd MyApp
javac -dir Classes Source/A.java

15. What is the result of attempting to compile and run the program?

class A extends Thread {

public void run() {


System.out.println("A");
}
}

class Test {

public static void main(String[] args) {

A a = new A();
a.start();
a.run(); // Line 1
a.run(); //Line 2
}
}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. An IllegalThreadStateException is thrown at run-time.
d. A , A , A
e. None of the above

16.Select the method contains in Thread class (java.lang.Thread)

a. wait()
b. sleep(long millis, int nanos)
c. setAlive(boolean live)
d. sleep(int millis)
e. exit()

17. What is the output of this program?

class A {

public static void main(String[] args) {


A w = new A();
System.out.println(w);
}
public int hashCode() {
return 16;
}
}

a. 16
b. A
c. A@16
d. A@10
e. Compile Error

18.What is the output of this program?

class A {

A() {
System.out.println("Constructor A");
}

static {
System.out.println("A1");
}

{
System.out.println("A2");
}
}

class B extends A {

static {
System.out.println("B1");
}

{
System.out.println("B2");
}
}

class Test{

public static void main(String[] args) {

B b = new B();
}
}

a. A1 , A2 , Constructor A , B1 , B2
b. A1 , A2 , B1 , B2 , Constructor A
c. A1 , B1 , A2 , Constructor A , B2
d. A1 , B1 , A2 , Constructor A , B2
e. None of the above
19. What is the correct answer when you compile and run the following program?

class A {

static void m() throws RuntimeException {


throw new Y(); //Line 1
}

public static void main(String[] args) {


System.out.println("A"); // Line 2
m(); // Line 3
System.out.println("B");
throw new Y(); //Line 4
System.out.println("C"); // Line 5
}
}

a. Compile Error At Line 1


b. Compile Error At Line 2
c. Compile Error At Line 3
d. Compile Error At Line 4
e. Compile Error At Line 5

20. How to create A.jar file from A.class

a. jar jf A.jar A.class


b. jar jr A.jar A.class
c. jar cf A.jar A.class
d. jar jr A.jar frm A.class

21.What is the output of this program?

import java.util.ArrayList;

class A {

public static void main(String[] args) {

ArrayList<Integer> al = new ArrayList();


al.add(new Integer("10")); //Line 1
al.add(new Integer(20)); //Line 2
al.add(30); //Line 3

System.out.println(al);
}
}

a. Compile error at Line 1


b. Compile error at Line 2
c. Compile error at Line 3
d. Runtime Exception
e. [10, 20, 30]
22.What is the output of this program?

class Test {

public static void main(String args[]) {


try {
System.out.println("Java " + " " + 10 / 0);
} catch (ArithmeticException e) {
System.out.println("PCJT");
}
}
}

a. Compile Error
b. Runtime Exception
c. PCJT
d. Java , ArithmeticException , PCJT
e. Java PCJT

23.How to convert Date object to String?

a. SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-mm-dd”);


sdf.parse(new Date());

b. SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-mm-dd”);


sdf.format(new Date());

c. SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-mm-dd”);


new Date().parse();

d. SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-mm-dd”);


new Date().format();

e. None of the above

24. What is the order of variables in Enum?

a. Ascending order
b. Descending order
c. Random order
d. depends on the order() method
e. None of the above

25.What is the output of this program?

class Test {

public static void main(String args[]) {


String s1 = "PCJT";
String s2 = new String(s1);
System.out.println((s1 == s2) + " " + s1.equals(s2));
}
}

a. true , false
b. false , true
c. true , true
d. false , false
e. Compile error

26.Which of these class is used to read from byte array?

a. InputStream.
b. BufferedInputStream.
c. ArrayInputStream.
d. ByteArrayInputStream.
e. None of the above
27.What is the output of this program?

import java.util.*;

class stack {

public static void main(String args[]) {

Stack s = new Stack();


s.push(new Integer(3));
s.push(new Integer(2));
s.pop();
s.push(new Integer(5));
s.peek();

System.out.println(s);
}
}

a. [3, 5].
b. [3, 2].
c. [3, 2, 5].
d. [3, 5, 2].
e. [2,5]

28. What is the output of this program?

class T implements Runnable {

Thread t;

T() {
t = new Thread(this, "My Thread");
t.start();
}

public void run() {


System.out.println(t);
}
}

class Test {

public static void main(String args[]) {


new T();
}
}

29. Why are generics used?

a. Generics make code more fast


b. Generics make code more optimised and readable
c. Generics add stability to your code by making more of your bugs detectable at compile time
d. Generics add stability to your code by making more of your bugs detectable at run time

30. What is the correct method used to insert and delete items from queue?

a. push and pop


b. enqueue and dequeue
c. enqueue and peek
d. add and remove
e. push and poll
31. What is the output of this program?

class Test {

public static void main(String args[]) {


Thread t = Thread.currentThread();
t.setName("T1");
System.out.println(t);
}
}

a. Thread[5,T1]
b. Thread[T1,5]
c. Thread[T1,5,main]
d. Thread[T1,5,T1]
e. Thread[main,0,T1]

32. What is the output of this program?

import java.util.*;

class Test {

public static void main(String args[]) {

TreeMap t = new TreeMap();


t.put("C", new Integer(1));
t.put("A", new Integer(2));
t.put("C", new Integer(3));
t.put("C", null);
System.out.println(t.entrySet());
}
}

a. Runtime Exception
b. [A,C]
c. [1,2,3,null]
d. [null,1,2,3]
e. [A=2,C=null]
f. [A=2,C=1,C=3,C=null]

33.What is the output of this program?

class Test {

public static void main(String args[]) {


Double i = new Double(237.567);
int x = i.intValue();
System.out.print(x);
}
}

a. Runtime Exception
b. 238
c. 237
d. 567
e. 24

34.Which capturing group can represent the entire expression?

a. group *
b. group 0
c. group * or group 0
d. group ()
e. Non of the above
35.Which of these classes can return more than one character to be returned to input stream?

a. BufferedReader
b. Bufferedwriter
c. StringReader
d. CharArrayReader
e. InputReader

36.What is the output of this program?

class Test
{
public static void main(String args[])
{
Long i = new Long(256);
System.out.print(i.hashCode());
}
}

a. 256
b. 256.0
c. 256.00
d. 257.00
e. Runtime exception.

37.Which method returns the elements of Enum class?

a. getEnums()
b. getEnumConstants()
c. getEnumList()
d. getEnum()
e. None of the above

38.What is the output of this program?

import java.util.StringTokenizer;

class Test {

public static void main(String[] args) {

StringTokenizer st = new StringTokenizer("Apple,HTC,Sony,Huawei");


System.out.println("Next token is : " + st.nextToken(","));
System.out.println("Next token is : " + st.nextToken(","));
}
}

a. Compile Error
b. Runtime Exception
c. Apple , Apple
d. Apple , HTC
e. null , null
39. How the below program can be end?

class A extends Thread {

public void run() {

for (int i = 0; i < 10; i++) {

try {
System.out.println("A=" + i);
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

class B {

public static void main(String[] args) {

A a = new A();
a.setDaemon(true);
a.start();

for (int x = 0; x < 3; x++) {


try {
System.out.println("Main=" +x);
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

1.When i = 0
2.When i = 10
3.When x = 0
4.When x = 3
5.None of Above

40.What is the output of this program?

class A {

A() {
this(10);
System.out.println("A");
}

A(int x) {
System.out.println("X : " + x);
}

static {
System.out.println("A 1");
}
}

class B extends A {

static {
System.out.println("B 1");
}
{
System.out.println("B 2");
}

public static void main(String[] args) {


B b1 = new B();
B b2 = new B();
}
}

a. Compile Error
b. B 1 , A 1 , A , A1
c. A 1 , B 1 , X : 10 , A , B 2 , X : 10 , A , B 2
d. A 1 , B 1 , X : 10 , A , B 2 , A 1 , B 1, X : 10 , A , B 2
e. A 1 , B 1 , A , X : 10 ,B 1

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

enum X {
int n = 10;
A, B, C;
}

class A {

public static void main(String[] args) {


System.out.println(X.A.n + X.B.n);
}
}

1.10
2.20
3.Compile Error At enum “X”
4.Compile Error At class “A”
5.1010

42. Which of the following classes can catch all exceptions which cannot be caught?

a. RuntimeException
b. Error
c. Exception
d. ParentException
e. None of the above

Write Short Answers To Question 43,44 & 45

43. Write the contract between equals(); and hashCode(); in Java.[2 Marks]

44. Write three useful methods in Map interface. [3 Marks]

45. What is the behaviour of yield(); method? [3 Marks]


Object Oriented Programming Concept II – Part II (Structured Questions)
Section A (Answer The All Questions)
01. Modify the code below and write a Test class to create a soldier named “Nirodha” and then save your soldier
object in a file called “Soldier.ser” in the class directory.

class Soldier {

String name;

public void run() {


System.out.println("Soldier Running");
}
}

02. Write a program to create four students with a NIC and a name. Then store them in a collection and sort them in
two ways:
i. Using their NIC number
ii. Using their name

03. Use the code below and create a subclass of a Dog class named “LionShepherd”. Then modify the bark();
method and implement the thread behavior to the “LionShepherd” class.
Write a Test class to execute the “LionShepherd” class as a separate thread to call the bark(); method.

class Dog {

public void bark() {


System.out.println("Dog Bark");
}
}

Section B (Answer Only One questions)

04. Create your own checked exception called “UpsetException” and an unchecked exception called
“CharterException”. By using these two exceptions, write a program to explain the difference between checked
and unchecked exceptions.

05. Write a code example to explain the “LIFO” collection. Explain the useful methods in this collection using your
code example.

Section C (Answer Only One questions)

06. Explain the contrast between equals(); & hashCode(); methods in Java.

Modify the code below and write a Test class to prove that the customized Gun objects, which has the same bullets, are
equals.

class Gun {

int bullet;

public Gun(int bullet) {


this.bullet = bullet;
}
}

07. Write a code example to explain the Anonymous Inner class by using the code below.

interface Animal {

public abstract void eat();


public abstract void run();
}
OOPC II
01 g
02 e
03 4
04 e
05 e
06 d
07 e
08 e
09 a,c,e
10 a
11 b
12 1
13 e
14 4
15 d
16 a,b,d
17 d
18 c,d
19 a
20 c
21 e
22 c
23 b
24 e
25 b
26 b
27 a
28 Thread[My Thread,5,main]
29 c
30 a
31 c
32 e
33 c
34 e
35 a,b
36 a
37 b
38 d
39 5
40 d
41 3
42 e

43.
If two objects are equal according to the equals(Object) method, then calling thehashcode() method on each of the two
objects must produce the same integer result.
44.
put(Object key,Object value);
remove(Object key);
size();
45.
When a thread calls yield () method, this means, the thread wants to relinquish the control over processor and give it to other
priorities threads while not doing anything useful / waiting for input. In a simple word when you call Thread.yield(), thread
leaves CPU and goes back to queue and wait.

Object Oriented Programming Concept II – Part II (Structured Questions)


Section A (Answer The All Questions)
01. Modify the code below and write a Test class to create a soldier named “Nirodha” and then save your
soldier object in a file called “Soldier.ser” in the class directory.

class Soldier {

String name;

public void run() {


System.out.println("Soldier Running");
}
}

Answer:

class Soldier implements Serializable {

String name;

public Soldier(String a) {
this.name = a;
}

public void run() {


System.out.println("Soldier Running");
}

class Test1{

public static void main(String[] args) {


try {
File f=new File("Soldier.ser");
FileOutputStream fo=new FileOutputStream(f);
ObjectOutputStream os=new ObjectOutputStream(fo);
os.writeObject(new Soldier("Nirodha"));
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
02. Write a program to create four students with a NIC and a name. Then store them in a collection and sort
them in two ways:
i. Using their NIC number
ii. Using their name

Answer:

public class Student {


String nic;
String name;
Student(String nic1,String name1){
this.nic=nic1;
this.name=name1;
}
}

public class C1 implements Comparator<Student> {


public int compare(Student s1,Student s2){
return s1.name.compareTo(s2.name);
}

}
public class C2 implements Comparator<Student>{
public int compare(Student s1,Student s2){
return new Integer(s1.nic).compareTo(new Integer(s2.nic));
}

}
public class TestDemo {

public static void main(String[] args) {


ArrayList<Student> a = new ArrayList<Student>();
Student s1 = new Student("123", "Abc");
Student s2 = new Student("456", "Def");
a.add(s1);
a.add(s2);
Collections.sort(a, new C1());
System.out.println(a);
Collections.sort(a, new C2());
System.out.println(a);
}
}

03. Use the code below and create a subclass of a Dog class named “LionShepherd”. Then modify the bark();
method and implement the thread behavior to the “LionShepherd” class.
Write a Test class to execute the “LionShepherd” class as a separate thread to call the bark(); method.

class Dog {

public void bark() {


System.out.println("Dog Bark");
}
}

Answer:

class Dog {

public void bark() {


System.out.println("Dog Bark");
}
}

class LionShepherd extends Dog implements Runnable {

@Override
public void run() {
bark();
}
public void bark() {
System.out.println("LionShepherd Bark");
}

}
class Test{

public static void main(String[] args) {


LionShepherd l=new LionShepherd();
Thread t=new Thread(l);
t.start();
}
}
Section B (Answer Only One questions)

04. Create your own checked exception called “UpsetException” and an unchecked exception called
“CharterException”. By using these two exceptions, write a program to explain the difference between checked
and unchecked exceptions.

Answer:

class Customer {

Customer(String name) {

class UpsetException extends Exception {

public UpsetException(String message) {


super(message);
}

class Test {

public Customer findByName(String name) throws UpsetException {

if ("".equals(name)) {
throw new UpsetException("Name is empty!");
}

return new Customer(name);

public static void main(String[] args) {

Test obj = new Test();

try {

Customer cus = obj.findByName("");

} catch (UpsetException e) {
e.printStackTrace();
}

}
}

Output: Name is empty!

public class CharterException extends RuntimeException {

public CharterException(String message) {


super(message);
}

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test1 {

public void analyze(List<String> data) {

if (data.size() > 50) {


//runtime exception
throw new CharterException("List can't exceed 50 items!");
}

//...
}

public static void main(String[] args) {

Test1 obj = new Test1();

//create 100 size


List<String> data = new ArrayList<>(Collections.nCopies(100, "mkyong"));

obj.analyze(data);

}
}

Output: List can't exceed 50 items!

05. Write a code example to explain the “LIFO” collection. Explain the useful methods in this collection using
your code example.

Answer:
import java.io.*;
import java.util.*;

class Test {
// Pushing element on the top of the stack
static void stack_push(Stack<Integer> stack)
{
for (int i = 0; i < 5; i++) {
stack.push(i);
}
}

// Popping element from the top of the stack


static void stack_pop(Stack<Integer> stack)
{
System.out.println("Pop :");

for (int i = 0; i < 5; i++) {


Integer y = (Integer)stack.pop();
System.out.println(y);
}
}

// Displaying element on the top of the stack


static void stack_peek(Stack<Integer> stack)
{
Integer element = (Integer)stack.peek();
System.out.println("Element on stack top : " + element);
}

// Searching element in the stack


static void stack_search(Stack<Integer> stack, int element)
{
Integer pos = (Integer)stack.search(element);

if (pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position " + pos);
}

public static void main(String[] args)


{
Stack<Integer> stack = new Stack<Integer>();

stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
Section C (Answer Only One questions)

06. Explain the contrast between equals(); & hashCode(); methods in Java.

Answer:
equals()

The java string equals () method compares the two given strings based on the content of the string. If any
character is not matched, it returns false. If all characters are matched, it returns true.

hashCode()

hashCode() is a function that takes an object and outputs a numeric value. The hashcode for an object is always
the same if the object doesn't change.

If o1.equals(o2), then o1.hashCode() == o2.hashCode() should always be true.

If o1.hashCode() == o2.hashCode is true, it doesn’t mean that o1.equals(o2) will be true

public static void main(String[] args) {

A objectA=new A();
B objectB=new B();
B ob=null;

String a="abc"; String b="abc";


String c="ABC";
String d="AbC";

System.out.println(objectA.equals(objectB));//false
System.out.println(a.equals(b));//true
System.out.println(a.equals(c));//false
System.out.println(a.equals(d));//false
System.out.println(objectA.hashCode()); //366712642 System.out.println(objectB.hashCode());
//1829164700
System.out.println(a.hashCode()); //96354
System.out.println(b.hashCode()); //96354
System.out.println(c.hashCode()); //64578
System.out.println(d.hashCode()); //65570
System.out.println(ob.hashCode()); // runtime error
System.out.println(ob.equals(objectB)); //runtime error

Modify the code below and write a Test class to prove that the customized Gun objects, which has the same bullets,
are equals.

class Gun {

int bullet;

public Gun(int bullet) {


this.bullet = bullet;
}
}

Answer:

class Gun {

int bullet;

public Gun(int bullet) {


this.bullet = bullet;
}

public Integer getBullet() {


return bullet;
}

class Ak47 extends Gun {

public Ak47(int bullet) {


super(bullet);
}

class T56 extends Gun {

public T56(int bullet) {


super(bullet);
}

public class Test {

public static void main(String[] args) {

Gun g = new Ak47(10);


Gun g1 = new T56(10);
System.out.println(g.getBullet().equals(g1.getBullet()));

}
}

07. Write a code example to explain the Anonymous Inner class by using the code below.

interface Animal {

public abstract void eat();


public abstract void run();
}

Answer:

interface Animal {

public abstract void eat();


public abstract void run();
}

public class Test {


public static void main(String[] args) {
Animal a1=new Animal() {

@Override
public void eat() {
System.out.println("eat");
}

@Override
public void run() {
System.out.println("run");
}
};
a1.eat();
a1.run();

}
}

You might also like