Oopc Ii Paper 3
Oopc Ii Paper 3
Oopc Ii Paper 3
class Test {
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
try {
System.out.println(i++);
Thread.sleep(1000); // Line 1
} catch (Exception e) {
e.printStackTrace();
}
}
}
A a1 = (A) a; // Line 3
a1.start();
}
}
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
import java.util.regex.Pattern;
class A{
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;
}
System.out.println(ss);
}
}
class Demo {
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{
}
}
a. final
b. synchronized
c. static
d. native
e. private
10. What is the output of this program?
class A {
a. true , true
b. true , false
c. false , true
d. Throw an Exception
e. Compile Error
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 {
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
class A {
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 Test {
A a = new A();
a.start();
a.run(); // Line 1
a.run(); //Line 2
}
}
a. wait()
b. sleep(long millis, int nanos)
c. setAlive(boolean live)
d. sleep(int millis)
e. exit()
class A {
a. 16
b. A
c. A@16
d. A@10
e. Compile Error
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{
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 {
import java.util.ArrayList;
class A {
System.out.println(al);
}
}
class Test {
a. Compile Error
b. Runtime Exception
c. PCJT
d. Java , ArithmeticException , PCJT
e. Java PCJT
a. Ascending order
b. Descending order
c. Random order
d. depends on the order() method
e. None of the above
class Test {
a. true , false
b. false , true
c. true , true
d. false , false
e. Compile error
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 {
System.out.println(s);
}
}
a. [3, 5].
b. [3, 2].
c. [3, 2, 5].
d. [3, 5, 2].
e. [2,5]
Thread t;
T() {
t = new Thread(this, "My Thread");
t.start();
}
class Test {
30. What is the correct method used to insert and delete items from queue?
class Test {
a. Thread[5,T1]
b. Thread[T1,5]
c. Thread[T1,5,main]
d. Thread[T1,5,T1]
e. Thread[main,0,T1]
import java.util.*;
class Test {
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]
class Test {
a. Runtime Exception
b. 238
c. 237
d. 567
e. 24
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
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.
a. getEnums()
b. getEnumConstants()
c. getEnumList()
d. getEnum()
e. None of the above
import java.util.StringTokenizer;
class Test {
a. Compile Error
b. Runtime Exception
c. Apple , Apple
d. Apple , HTC
e. null , null
39. How the below program can be end?
try {
System.out.println("A=" + i);
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class B {
A a = new A();
a.setDaemon(true);
a.start();
1.When i = 0
2.When i = 10
3.When x = 0
4.When x = 3
5.None of Above
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");
}
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 {
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
43. Write the contract between equals(); and hashCode(); in Java.[2 Marks]
class Soldier {
String name;
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 {
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.
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;
07. Write a code example to explain the Anonymous Inner class by using the code below.
interface Animal {
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.
class Soldier {
String name;
Answer:
String name;
public Soldier(String a) {
this.name = a;
}
class Test1{
Answer:
}
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 {
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 {
Answer:
class Dog {
@Override
public void run() {
bark();
}
public void bark() {
System.out.println("LionShepherd Bark");
}
}
class Test{
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 Test {
if ("".equals(name)) {
throw new UpsetException("Name is empty!");
}
try {
} catch (UpsetException e) {
e.printStackTrace();
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//...
}
obj.analyze(data);
}
}
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);
}
}
if (pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position " + pos);
}
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.
A objectA=new A();
B objectB=new B();
B ob=null;
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;
Answer:
class Gun {
int bullet;
}
}
07. Write a code example to explain the Anonymous Inner class by using the code below.
interface Animal {
Answer:
interface Animal {
@Override
public void eat() {
System.out.println("eat");
}
@Override
public void run() {
System.out.println("run");
}
};
a1.eat();
a1.run();
}
}