You are on page 1of 10

Programming

1) Write a Program to create an employee POJO class and compare employee based on id by overriding equals and
hash code method.
2) Explore some of the methods in String and StringBuffer. And apply equal method on String objects, Wrapper
Objects and also on custom objects.
3) Write a custom exception class, override the getMessage method and use custom exception to throw the
exception in your program.
4) File contains multiple rows as follows:
Eg: Id, ParentId, Name, Description
1,
0,
Device, Alcatel Device
2,
1,
Router, Router1300
3,
1,
Switch, Switch2001
4,
2,
P Ports, Physical Port
5,
2
L Ports, Logical Port
6,
3
P Ports, Physical Port
7,
3
L Ports, Logical Port
Read the data from the file and use appropriate data structure to store the data and it should persist parent
children relationship. Print all the childrens which are under level 2
5) Explore Serialization Inheritance Concept. Write a sample program to serialize and de-serialize the object.
6) Write block of code to get the lock on an object so that other threads cannot modify it while the block of code is
executing. Create 3 threads that will all attempt to manipulate the same object. Each thread will output a single
letter 100 times, and then increment that letter by one. Use StringBuffer(as String is immutable).
Eg O/P: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

Find the output or errors


Note: Assume all import statements are included and no syntax errors

Question
7) public class SingleArray { static void main (String args[])
{
int[] single= new int[4];
for (int i=0; i < 4; i++)
{single[i] = i; System.out.println(single[i]);
}
}
}
8) public class MultiArray { static void main (String args[])
{
int[][] multi;
multi = new int[4][5];
for (int row=0; row < 4; row++) {
for (int col=0; col < 5; col++) {
multi[row][col] = row + col;
System.out.println(row + "" + col);
}

Write Answer / Select Correct Options


Public static void main
0
1
2
3
Public static void main
00
01
02
03
04
10
11

option

}
}
}
9)
A) public class DefaultValues{
public int a ; public long b ; public float c ; public double d
public char e ; public byte f ; public Boolean g; }
Public class Demo{
Public static void main(String args[]){
DefaultValues obj=new DefaultValues();
System.out.println(a=+obj.a+ b=+obj.b+
c=obj.c);
System.out.println(d=+obj.d+ e=+obj.f+
g=obj.g);
}
}
B) public class DefaultValues{ Object ref; }
Public class Demo{
Public static void main(String args[]){
DefaultValues obj=new DefaultValues();
System.out.println(obj.ref);
}
}
10) public class StaticTest {
static String s= "Hello i am static";
public int x ;
public static void main ( String args[] ) {
System.out.println (s) ;
System.out.println (x);
}
}
11)
A) public class NonStaticTest {
static int x = 10;
public static void main ( String args[] ) {
NonStaticTest nt = new NonStaticTest() ;
NonStaticTest.x = 20;
NonStaticTest nt1 = new NonStaticTest() ;
nt1.x = 30;
System.out.println (nt.x); System.out.println
(nt1.x);
}
}
B) public class NonStaticTest {
static int x = 10; int y;
NonStaticTest(int a,int b)
{ x=a; y=b; }
public static void main ( String args[] ) {
NonStaticTest nt = new NonStaticTest(20,20)
;
NonStaticTest nt1 = new
NonStaticTest(30,30) ;
System.out.println(nt.x);
System.out.println(nt1.x);
System.out.println(nt.y);

A) a=0 b=0 c=0 d=0


E=0 f=0 g=null

B) 1)
2)
3)
4)

hashcode
garbage Values
NullPointerException
null

1) Hello I am static 0
2) Hello I am static NullPointerException
3) compile time error, non-static features cannot be
called
4) Runtime Error

A)
30
30

B)30
30
20
30

System.out.println(nt1.y);
}
}
12) Fill Yes (Or) No wherever applicable
Modifier
Variable Metho Class
Inner
d
Class
Public
Yes/No
Yes/No Yes/N Yes/N
o
o
Private
Yes/No
Yes/No Yes/N Yes/N
o
o
Protected
Yes/No
Yes/No Yes/N Yes/N
o
o
Final
Yes/No
Yes/No Yes/N Yes/N
o
o
Abstract
Yes/No
Yes/No Yes/N Yes/N
o
o
Static
Yes/No
Yes/No Yes/N Yes/N
o
o
Native
Yes/No
Yes/No Yes/N Yes/N
o
o
Transient
Yes/No
Yes/No Yes/N Yes/N
o
o
Volatile
Yes/No
Yes/No Yes/N Yes/N
o
o
Synchronize Yes/No
Yes/No Yes/N Yes/N
d
o
o
13) class Top{ Top(double d) { System.out.println(d); } }
public class Down extends Top {
public static void main(String args[]) {
for ( int i = 0 ; i < 5; i++ )
Down d = new Down();
System.out.println(i);
}
}
14) class A {
A() {
System.out.println("Class A Constructor");
}
}
public class B extends A {
B() {
System.out.println("Class B Constructor");
}
public static void main(String args[]) {
B b = new B();
}
}
15) Given the piece of code, select the correct answer to
replace at the comment line //xxxxx ?
class A {
A(int i) { }
public class B extends A {
B() { // xxxxx }

Modifier

Variable

Method

Class

Public
Private
Protected
Final
Abstract
Static
Native
Transient
Volatile
Synchronized

y
y
y
y
N
y
n
y
y
n

y
y
y
y
y
y
y
n
n
y

y
n
n
y
y
n
n
n
n
n

Inner
Class
y
y
y
y
y
y
n
n
n
n

2
1)
2)
3)
4)

0 1 2 3 4 5
Compile time error call to default constructor
0 1 2 3
1 2 3 4 5

1
1)
2)
3)
4)

Class A Constructor
Class B Constructor
Compile time error
Run time error

Class B Constructor
Class A Constructor

1
1) super(100);
2) this(100);
3) super();

public static void main(String args[]) {


B b = new B();
}

4) this();

}
}
16) int x;
for ( x = 0, int y=0; x < 3&&y<2 ; x++,y++) {
System.out.println(0);
}
17) public class TestBreak {
public static void main(String args[]) {
int val[] = new int[]{0,1,2,3,4,5};
int x;
for ( x=0; x < val.length; x++) {
if (val[x] == 0) {
System.out.println( val[x]);
break;
}
}
System.out.println(val[x]);
}
}
18) public class TestContinue {
public static void main(String args[]) {
for ( int i = 0 ; i <2; i++) {
for ( int x=0; x<3; x++) {
if ( i == x) {
continue;
}
System.out.println(i + " " + x);
}
}
}
}
19) public class TestContinue {
public static void main(String args[]) {
int x;
FirstLoop: for ( int i = 0 ; i<2; i++) {
for ( x=0; x<3; x++) {
if ( i == x) { continue
FirstLoop; }
System.out.println(i + " " + x);
}
}
}
}
20) public class TestExp {
public static void main(String args[]) {
try {
int val = 0;
val = Integer.parseInt(args[0]);
System.out.println("No Exception is Raised");
}
catch(NumberFormatException ne) {

1)
2)
3)
4)

0000
000
00
Error: comma separation cannot be mixed with
variable declarations in a for loop

3- 0 0
1)
2)
3)
4)

01 2 3 4
1 2 3 4 5
0 0
0 5

01
02
10
12

3
1)
2)
3)
4)

01 12
12
10
Error

A) Exception is caught, Executing finally block

B) No exception is raised , Executing finally block

System.out.println("Exception is Caught");
}
finally {
System.out.println("Executing finally block");
}
}

C) Executing finally block , arrayindex out of bounds

}
Write output of above sample code when
A) argument is other than int/string containing only
numbers value
B) argument is int/string containing only number value
C) no argument is passed
21) public class TestExp { public static void main(String
args[]) {
try { System.exit(); system.out.print( No
Exception); }catch(NumberFormatException ne) {
System.out.println("Exception is Caught"); }
finally { System.out.println("Executing fillay
block"); }
}
}
22) public class ThrowTest {
static void throwTest() {
try {throw new NullPointerException("ThrowTest");}
catch(NullPointerException ne) {
System.out.println("Caught inside throwTest.");
throw ne;
}
}
public static void main(String args[]) {
try { throwTest();
}
catch(NullPointerException ne) {
System.out.println("Recaught ");
}
}
}
23) How can you change the break statement below so that it
breaks out of the inner continues with the next iteration of
the outer loop?
outer: for ( int x =0; x < 3; x++ ) {
middle: for ( int y=0; y < 3; y++ ) {
if ( y == 1)
break;
}
}
24) class MyExp {
void MyMethod() throws IOException,
EOFException { //............ }
}
class MyExp1 extends MyExp {
void MyMethod() { //..........
}
}
class MyExp2 extends MyExp1 {

4
1)
2)
3)
4)

No Exception Executing finally block


Exception is Caught Executing finally block
Executing finally block
No output

Caught inside throwtest


Recaught

2
1)
2)
3)
4)
5)

break inner:
break middle:
break outer:
continue
continue middle

A) When try to compile given source code?


1) No compile time error
2) Run-Time error
3) MyMethod() cannot throw an exception in
MyExp2 class
B) How to correct given code ?

A) 3

void MyMethod() throws IOException{ //......... }

1) By adding throws for MyMethod in MyExp1


2) By removing throws in MyExp2
3) Any of the above

}
25) public class ExpTest {
public static void main ( String[] args ) {
try {MyMethod(); } catch ( Exception e) { }
}
static void MyMethod() {
try { System.out.println("a");
} catch ( ArithmeticException ae) {
System.out.println("b");}
finally { System.out.println("c"); }
System.out.println("d");
}
}
26) How can you force garbage collection?

27) Which statements about garbage collection are true?


(Note: select more than one option if applicable)

28) public class OuterClass {


private String s = "Outer class member variable";
class InnerClass {
private String s1 = "Inner class variable";
public void innerMethod() {
System.out.println(s);
System.out.println(s1);
}
}
Public static void outerMethod() {
InnerClass inner = new InnerClass();
inner.innerMethod();
}
public static void main( String args[] ) {
OuterClass.outerMethod();
}
}
29) public class OuterClass {
private String s = "Outer class member variable";
class InnerClass {
private String s1 = "Inner class variable";
public void innerMethod() {
System.out.println(s);
System.out.println(s1);
}
}
public static void outerMethod() {

B) 3
3

1)
2)
3)
4)

a
a
a
a

b c d
c
c d
b d

1)
2)
3)
4)
1)
2)

Garbage collection cannot be forced


Call System.gc().
Call Runtime.gc().
Set all references to null.
The garbage collector runs in low memory situations
You can run the garbage collector whenever you
want.
3) When it runs it releases the memory allocated by an
object.
4) Garbage collector immediately runs when you set
the references to null.
1) Outer class member variable
Inner class member variable
2) Outer class member variable
3) Inner class member variable
4) Error: No enclosing instance of type OuterClass
is accessible

1,2

A)4

B)

1
1) Outer class member variable
Inner class member variable
2) Outer class member variable
3) Inner class member variable
4) Error: No enclosing instance of type OuterClass is
accessible

OuterClass.InnerClass inner = new OuterClass().new


InnerClass();
inner.innerMethod();
}
public static void main( String args[] ) {
OuterClass.outerMethod();
}
}
30) public class OuterClass {
final String s = "Outer class member variable";
public void outerMethod() {
String s1 = "Outer class method variable";
class InnerClass {
public void innerMethod() { int xyz = 20;
System.out.print(s);
System.out.println("Integer value is " + xyz);
System.out.println(s1); }
}
}
public static void main( String args[] ) {
new OuterClass.outerMethod();
}
}
31)
A) int String=10;
B) boolean Boolean=true;
C) int #ab=50;
D) int $x=30;
E) boolean _y=false;
F) int $AB=100;
G) int string=60;
H) int 2sd=70;
Which of the above declarations are not correct?
32) Boolean, boolean, Switch, if, for, null, Integer, NULL, If
Which of the above are not valid keywords?
33) public class TestShift {
public static void main(String args[]) {
int x = 1, y = -1, z=-1;
x = x >> 31;
y = y >> 31;
z=z>>> 31;
System.out.println("The ouput of x is " + x );
System.out.println("The ouput of y is " + y );
System.out.println("The ouput of z is " + z );
x = 1, y = -1, z=1;
x = x << 31;
y = y << 31;
z=z>>> 31;
System.out.println("The ouput of x is " + x );
System.out.println("The ouput of y is " + y );
System.out.println("The ouput of z is " + z );
}
}
Note: 2 pow 31=2147483648

4
1) Outer class member variable
Integer value is 20
Outer class method variable
2) Runtime error: final variable s cannot be accessed
3) Compile time error : Cannot refer to a non-final
variable s1 inside an inner class defined in a
different method
4) Compiler err- cannot make static ref to non static
members.

3
1)
2)
3)
4)
5)
6)

Only A,B,C,G
All
Only C,H
Only A,B
Only C,D,E,F
Only H

Boolean,Switch,If,NULL
Error in line
X=1,y=-1,z=1;
Else
0
-1
1
-2147483648
-2147483648
0

34) which of the following methods could be legally placed


after the comment?
public class Test{
public void myMethod(int i, String s){}
//Here
}
A) public void myMethod(String s, int i){}
B) public int myMethod(int i, String s){}
C) public void myMethod(int i, String mystring){}
D) public void method(int i, String s) {}
35) public class SimpleThread extends Thread {
public void run ( ){
System.out.println ( "Executing simple
Thread");
}
public static void main ( String args[]) {
SimpleThread st = new SimpleThread();
st.start( );
}
}
36) public class RThread implements Runnable {
public void run (String s ) {
System.out.println ("Executing Runnable Interface
Thread") }
public static void main ( String args[]) {
RThread rt = new RThread();
Thread t = new Thread (rt); t.start();
}
}
37) Which of the following statements are true?
A) To create a new Thread, either implements Runnable
interface or extend Thread class
B) Thread start() method makes it eligible to run
C) Thread body lies in run() method.
D) Thread dies after completing the start() method
38) Which of the following statements are true?
A) Each and every object has a lock
B) Synchronized is used to give access to more than one
Thread to access a single resource.
C) wait()/notify() always need to call from try/catch block
D) notifyAll() method moves all the waiting objects to
Ready state
39) public class TestAbs { static void main(String args[]) {
System.out.println(Math.abs(145));
System.out.println(Math.abs(-145));
System.out.println(Math.abs(145.0));
System.out.println(Math.abs(-145.0));
System.out.println(Math.abs(145.0f));
System.out.println(Math.abs(-145.0f));
}
}
40) public class TestCeil { static void main(String args[]) {
System.out.println(Math.ceil(145));
System.out.println(Math.ceil(145.0));

4
1)
2)
3)
4)
5)
6)

Only A
Only B
Both A and C
Both A and D
Both B and C
Both B and D

Executing simple thread

Compiler error

4
1)
2)
3)
4)

Only A
B , C, D
A, B, C, D
A, B, C

1)
2)
3)
4)

Only A
B , C, D
A, B, C, D
A, C, D

145
145
145.0
145.0
145.0
145.0

145.0
145.0
146.0

System.out.println(Math.ceil(145.1));
System.out.println(Math.ceil(145.6));
System.out.println(Math.ceil(-145.4));
}
}
41) public class TestFloor { static void main(String args[]) {
System.out.println(Math.floor(145));
System.out.println(Math.floor(145.1));
System.out.println(Math.floor(145.8));
System.out.println(Math.floor(-145.0));
System.out.println(Math.floor(-145.4));
}
}
42) public class TestMax { static void main(String args[]) {
System.out.println(Math.max(145, 159.0));
System.out.println(Math.max(145.1, 139.04));
System.out.println(Math.max(-145.4, -159.1));
System.out.println(Math.min(145, 159.0));
System.out.println(Math.min(145.1, 139.04));
System.out.println(Math.min(-145.4, -159.1));
}
}
43) public class TestRound { static void main(String args[]) {
System.out.println(Math.round(145.0));
System.out.println(Math.round(145.1));
System.out.println(Math.round(145.7));
System.out.println(Math.round(-145.4));
System.out.println(Math.round(-145.7));
}
}
44) public class TestBString { static void main(String args[]){
StringBuffer s = new StringBuffer("Hello");
StringBuffer s2= new StringBuffer("Hello");
String s1 = "Hello";
if (s.equals(s2))
System.out.println("A");
if(s1.equals(s2)) System.out.println("B");
else System.out.println("C");
}
}
45) public class TestBString { static void main(String args[]){
Integer a = new Integer(10); int b = 10;
if ( a == b) System.out.println("int Equal using == ");
if (a.equals(b)) System.out.println("Equal using
equals()");
Double d = new Double("5.78");
Double d1 = new Double("5.78");
If(d==d1) System.out.println("Double == Equal");
if (d.equals(d1)) System.out.println("Double
Equal");
else System.out.println("Not Equal");
}
}
46) public class TestQueue { static void main(String args[]) {
LinkedList queue = new LinkedList();
queue.addFirst("Bill");

146.0
-145.0

145.0
145.0
145.0
-145.0
-146.0

159.0
145.1
-145.4
159.0
145.1
-145.4

145
145
146
-145
-146

4
1)
2)
3)
4)

A
A, B
A, C
C

Int Equal using ==


Equal using equals()
Double equal

Gates, Brian, Bill, Henry Ford, Bill


Gates, Brian, Bill

queue.addFirst("Henrry Ford");
queue.addFirst("Bill");
queue.addFirst("Brian");
queue.addFirst("Gates");
System.out.println(queue);
queue.removeLast();
queue.removeLast();
System.out.println(queue);
47)

48)

49)

50)

}
}
public class TestStack { static void main(String args[]) {
LinkedList stack = new LinkedList();
stack.addLast("Bill");
stack.addLast("Henrry Ford");
stack.addLast("Bill");
stack.addLast("Brian");
stack.addLast("Gates");
System.out.println(stack);
stack.removeFirst();
stack.removeFirst();
System.out.println(stack);
}}
Which of the following can be performed using the File
class?
A) Change the current directory
B) To get the name of the parent directory
C) Delete a file
D) Find if a file contains text or binary information
Which of the following used to read and write to network
sockets, which are super classes of Low level streams?
A) InputStream
B) StreamReaders, Writers
C) OutputStream
D) Streams ,Readers
What is the permanent effect on the file system of writing
data to a new FileWriter("report"), given the file report
already exists?

51) Choose all valid forms of the argument list for the
FileOutputStream constructor shown below?
A) FileOutputStream( FileDescriptor fd )
B) FileOutputStream( String n, boolean a )
C) FileOutputStream( boolean a )
D) FileOutputStream()
E) FileOutputStream( File f )

Bill, Henry Ford, Bill, Brian, Gates


Bill, Brian, Gates

1)
2)
3)
4)
5)

A
B
A,B,D
B,C
B,C,D

1)
2)
3)
4)
5)

A, B
B, C
D, E
E, F
A, C

1) The data is appended to the file.


2) The file is replaced with a new file.
3) A checked exception is raised as the file already
exists.
4) A runtime exception is raised as the file already
exists.

2
1)
2)
3)
4)
5)
6)

A, B, D
A, B, E
A, D, E
B, D, E
D, E
A, E