Professional Documents
Culture Documents
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
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);
}
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);
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();
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
System.out.println("Exception is Caught");
}
finally {
System.out.println("Executing finally block");
}
}
}
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)
2
1)
2)
3)
4)
5)
break inner:
break middle:
break outer:
continue
continue middle
A) 3
}
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?
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)
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
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
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
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
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 )
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
2
1)
2)
3)
4)
5)
6)
A, B, D
A, B, E
A, D, E
B, D, E
D, E
A, E