Freshers Resource Center
Freshers Resource Center
com
Principles.
Encapsulation:
Is the Mechanism that binds together code and the
data it manipulates, and keeps both safe from
outside interference and misuse.
Inheritance:
Is the process by which one object acquires the
properties of another object.
Polymorphism:
Is a feature that allows one interface to be used for
a general class of actions.
a)True
b)False
Ans: a.
14) Which operator is to create and concatenate
string?
Ans: Addition operator(+).
15) Which of the following declare an array of
string objects?
String[ ] s;
String [ ]s:
String[ s]:
String s[ ]:
Ans : a, b and d
16) What is the value of a[3] as the result of the
following array declaration?
1
2
3
4
Ans : d
17) Which of the following are primitive types?
byte
String
integer
Float
Ans : a.
18) What is the range of the char type?
0 to 216
0 to 215
0 to 216-1
0 to 215-1
Ans. d
19) What are primitive data types?
Ans : byte, short, int, long
float, double
boolean
char
20) What are default values of different primitive
types?
Ans : int - 0
short - 0
byte - 0
long - 0 l
float - 0.0 f
double - 0.0 d
boolean - false
char - null
21) Converting of primitive types to objects can be
explicitly.
a)True
b)False
Ans: b.
22) How do we change the values of the elements
of the array?
Ans : The array subscript expression can be used
to change the values of the elements of the array.
23) What is final varaible?
Ans : If a variable is declared as final variable, then
you can not change its value. It becomes constant.
24) What is static variable?
Ans : Static variables are shared by all instances of
a class.
Operators
1) What are operators and what are the various
types of operators available in Java?
5
7
9
Ans : c.
8) Is &&= a valid operator?
Ans : No.
9) Can a double value be cast to a byte?
Ans : Yes
10) Can a byte object be cast to a double value ?
Ans : No. An object cannot be cast to a primitive
value.
11) What are order of precedence and
associativity?
Ans : Order of precedence the order in which
operators are evaluated in expressions.
Associativity determines whether an expression is
evaluated left-right or right-left.
12) Which Java operator is right associativity?
Ans : = operator.
13) What is the difference between prefix and
postfix of -- and ++ operators?
Ans : The prefix form returns the increment or
decrement operation and returns the value of the
increment or decrement operation.
The postfix form returns the current value of all of
the expression and then
performs the increment or decrement operation on
that value.
14) What is the result of expression 5.45 + "3,2"?
The double value 8.6
The string ""8.6"
The long value 8.
The String "5.453.2"
Ans : d
Control Statements
1) What are the programming constructs?
Ans: a) Sequential
b) Selection -- if and switch statements
c) Iteration -- for loop, while loop and do-while loop
2) class conditional {
public static void main(String args[]) {
int i = 20;
int j = 55;
int z = 0;
z = i < j ? i : j; // ternary operator
System.out.println("The value assigned is " + z);
}
}
What is output of the above program?
Ans: The value assigned is 20
3) The switch statement does not require a break.
a)True
b)False
Ans: b.
4) The conditional operator is otherwise known as
the ternary operator.
a)True
b)False
Ans: a.
5) The while loop repeats a set of code while the
condition is false.
a)True
b)False
Ans: b.
6) The do-while loop repeats a set of code atleast
once before the condition is tested.
a)True
b)False
Ans: a.
7) What are difference between break and
continue?
Ans: The break keyword halts the execution of the
current loop and forces control out of the loop.
The continue is similar to break, except that
instead of halting the execution of the loop, it
starts the next iteration.
Ans: new.
13) Which method is used to garbage collect an
object?
Ans: finalize ().
14) Constructors can be overloaded like regular
methods.
a)True
b)False
Ans: a.
15) What is casting?
Ans: Casting is bused to convert the value of one
type to another.
Ans: a.
20) Which cast must be used to convert a larger
value into a smaller one?
Ans: Explicit cast.
21) Which cast must be used to cast an object to
another class?
Ans: Specific cast.
22) Which of the following features are common to
both Java & C++?
A.The class declaration
b.The access modifiers
c.The encapsulation of data & methods with in
objects
d.The use of pointers
Ans: a,b,c.
23) Which of the following statements accurately
describe the use of access modifiers within a class
definition?
a.They can be applied to both data & methods
b.They must precede a class's data variables or
methods
c.They can follow a class's data variables or
methods
d.They can appear in any order
e.They must be applied to data variables first and
then to methods
Ans: a,b,d.
24) Suppose a given instance variable has been
declared private.
Can this instance variable be manipulated by
methods out side its class?
a.yes
b.no
Ans: b.
Ans: a,b,c.
29) If no input parameters are specified in a
method declaration then the declaration will
include __.
a.an empty set of parantheses
b.the term void
Ans: a.
30) What are the functions of the dot(.) operator?
a.It enables you to access instance variables of any
objects within a class
b.It enables you to store values in instance
variables of an object
c.It is used to call object methods
d.It is to create a new object
Ans: a,b,c.
31) Which of the following can be referenced by
this variable?
a.The instance variables of a class only
b.The methods of a class only
c.The instance variables and methods of a class
Ans: c.
32) The this reference is used in conjunction with
___methods.
a.static
b.non-static
Ans: b.
33) Which of the following operators are used in
conjunction with the this and super references?
a.The new operator
b.The instanceof operator
c.The dot operator
Ans: c.
34) A constructor is automatically called when an
object is instantiated
a. true
b. false
Ans: a.
35) When may a constructor be called without
specifying arguments?
a. When the default constructor is not called
b. When the name of the constructor differs from
that of the class
c. When there are no constructors for the class
Ans: c.
36) Each class in java can have a finalizer method
a. true
b.false
Ans: a.
37) When an object is referenced, does this mean
that it has been identified by the finalizer method
for garbage collection?
a.yes
b.no
Ans: b.
38) Because finalize () belongs to the
java.lang.Object class, it is present in all ___.
a.objects
b.classes
c.methods
Ans: b.
39) Identify the true statements about finalization.
a.A class may have only one finalize method
b.Finalizers are mostly used with simple classes
c.Finalizer overloading is not allowed
Ans: a,c.
40) When you write finalize() method for your
class, you are overriding a finalizer
inherited from a super class.
a.true
b.false
Ans: a.
41) Java memory management mechanism
garbage collects objects which are no longer
referenced
a true
b.false
Ans: a.
42) are objects referenced by a variable
candidates for garbage collection when the
variable goes out of scope?
a yes
b. no
Ans: a.
43) Java's garbage collector runs as a ___ priority
thread waiting for __priority threads to relinquish
the processor.
a.high
b.low
Ans: a,b.
44) The garbage collector will run immediately
when the system is out of memory
a.true
b.false
Ans: a.
45) You can explicitly drop a object reference by
setting the value of a variable whose data type is a
reference type to ___
Ans: null
46) When might your program wish to run the
garbage collecter?
a. before it enters a compute-intense section of
code
Import statement.
Ans : Package
Ans : a.
28) Can variables be declared in an interface ? If
so, what are the modifiers?
Ans : Yes. final and static are the modifiers can be
declared in an interface.
29) What are the possible access modifiers when
implementing interface methods?
Ans : public.
30) Can anonymous classes be implemented an
interface?
Ans : Yes.
31) Interfaces can’t be extended.
a)True
b)False
Ans : b.
32) Name interfaces without a method?
Ans : Serializable, Cloneble & Remote.
33) Is it possible to use few methods of an
interface in a class ? If so, how?
Ans : Yes. Declare the class as abstract.
Exception Handling
1) What is the difference between ‘throw’ and
‘throws’ ?And it’s application?
Ans : Exceptions that are thrown by java runtime
systems can be handled by Try and catch blocks.
With throw exception we can handle the
exceptions thrown by the program itself. If a
method is capable of causing an exception that it
does not
handle, it must specify this behavior so the callers
of the method can guard
MULTI THREADING
1) What are the two types of multitasking?
Ans : 1.process-based
2.Thread-based
2) What are the two ways to create the thread?
Ans : 1.by implementing Runnable
2.by extending Thread
3) What is the signature of the constructor of a
thread class?
Ans : Thread(Runnable threadob,String
threadName)
4) What are all the methods available in the
Runnable Interface?
Ans : run()
5) What is the data type for the method isAlive()
and this method is
available in which class?
Ans : boolean, Thread
6) What are all the methods available in the
Thread class?
Ans : 1.isAlive()
2.join()
3.resume()
4.suspend()
5.stop()
6.start()
7.sleep()
8.destroy()
7) What are all the methods used for Inter Thread
communication and what is the class in which
these methods are defined?
Ans :1. wait(),notify() & notifyall()
2. Object class
8) What is the mechanisam defind by java for the
Resources to be used by only one Thread at a
time?
Ans : Synchronisation
9) What is the procedure to own the moniter by
many threads?
Ans : not possible
10) What is the unit for 1000 in the below
statement?
ob.sleep(1000)
Ans : long milliseconds
11) What is the data type for the parameter of the
sleep() method?
Ans : long
12) What are all the values for the following level?
max-priority
min-priority
normal-priority
Ans : 10,1,5
13) What is the method available for setting the
priority?
Ans : setPriority()
Inheritance
1) What is the difference between superclass &
subclass?
Ans : super.method-name(arguments)
12) Which methods are used to destroy the objects
created by the constructor methods?
Ans : finalize()
13) What are abstract classes?
Ans : Abstract classes are those for which
instances can’t be created.
14) What must a class do to implement an
interface?
Ans: It must provide all of the methods in the
interface and identify the interface in its
implements clause.
15) Which methods in the Object class are
declared as final?
Ans : getClass(), notify(), notifyAll(), and wait()
16) Final methods can be overridden.
True/False
Ans : False
17) Declaration of methods as final results in faster
execution of the program?
True/False
Ans: True
18) Final variables should be declared in the
beginning?
True/False
Ans : True
19) Can we declare variable inside a method as
final variables? Why?
Ans : Cannot because, local variable cannot be
declared as final variables.
20) Can an abstract class may be final?
Ans : An abstract class may not be declared as
final.
b)Implemented
c)unimplemented
Ans:c
STRING HANDLING
Which package does define String and StringBuffer
classes?
Ans : java.lang package.
Which method can be used to obtain the length of
the String?
Ans : length( ) method.
How do you concatenate Strings?
Ans : By using " + " operator.
Which method can be used to compare two strings
for equality?
Ans : equals( ) method.
Which method can be used to perform a
comparison between strings that ignores case
differences?
Ans : equalsIgnoreCase( ) method.
What is the use of valueOf( ) method?
Ans : valueOf( ) method converts data from its
internal format into a human-readable form.
What are the uses of toLowerCase( ) and
toUpperCase( ) methods?
System.out.println(s1+s2+s3);
}
}
abcdefabcdef
abcabcDEFDEF
abcdefabcDEF
None of the above
ANS : c.
Which of the following methods are methods of the
String class?
delete( )
append( )
reverse( )
replace( )
Ans : d.
Which of the following methods cause the String
object referenced by s to be changed?
s.concat( )
s.toUpperCase( )
s.replace( )
s.valueOf( )
Ans : a and b.
String is a wrapper class?
True
False
Ans : b.
17) If you run the code below, what gets printed
out?
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));
Bic
ic
c) icy
d) error: no method matching substring(int,char)
Ans : b.
18) Given the following declarations
String s1=new String("Hello")
EXPLORING JAVA.LANG
System.out.println(ten + nine);
int i=1;
System.out.println(i + ten);
19 followed by 20
19 followed by 11
Error: Can't convert java lang Integer
d) 10 followed by 1
Ans : c.
EVENT HANDLING
The event delegation model, introduced in release
1.1 of the JDK, is fully compatible with the
event model.
True
False
Ans : b.
A component subclass that has executed
enableEvents( ) to enable processing of a certain
kind of event cannot also use an adapter as a
listener for the same kind of event.
True
False
Ans : b.
What is the highest-level event class of the event-
delegation model?
Ans : The java.util.eventObject class is the highest-
level class in the event-delegation hierarchy.
What interface is extended by AWT event
listeners?
Ans : All AWT event listeners extend the
java.util.EventListener interface.
What class is the top of the AWT event hierarchy?
Ans : The java.awt.AWTEvent class is the highest-
level class in the AWT event class hierarchy.
What event results from the clicking of a button?
Ans : The ActionEvent event is generated as the
result of the clicking of a button.
What is the relationship between an event-listener
interface and an event-adapter class?
APPLETS
What is an Applet? Should applets have
constructors?
Ans : Applet is a dynamic and interactive program
that runs inside a Web page
displayed by a Java capable browser. We don’t
have the concept of Constructors in Applets.
False.
Ans : a.
What are the steps involved in Applet
development?
Ans : a) Edit a Java source file,
b) Compile your program and
c) Execute the appletviewer, specifying the name
of your applet’s source file.
Applets are executed by the console based Java
run-time interpreter.
True.
False.
Ans : b.
Which classes and interfaces does Applet class
consist?
Ans : Applet class consists of a single class, the
Applet class and three interfaces: AppletContext,
AppletStub and AudioClip.
What is the sequence for calling the methods by
AWT for applets?
Ans : When an applet begins, the AWT calls the
following methods, in this sequence.
init( )
start( )
paint( )
When an applet is terminated, the following
sequence of method cals takes place :
stop( )
destroy( )
Which method is used to output a string to an
applet?
Ans : drawString ( ) method.
Every color is created from an RGB value.
True.
False
Ans : a.
}
A circle at (100, 100) with radius of 44
A circle at (100, 44) with radius of 100
A circle at (100, 44) with radius of 44
The code does not compile
Ans : d.
8)What is relationship between the Canvas class
and the Graphics class?
Ans : A Canvas object provides access to a
Graphics object via its paint( ) method.
What are the Component subclasses that support
painting.
Ans : The Canvas, Frame, Panel and Applet classes
support painting.
What is the difference between the paint( ) and
repaint( ) method?
Ans : The paint( ) method supports painting via a
Graphics object. The repaint( ) method is used
to cause paint( ) to be invoked by the AWT painting
method.
What is the difference between the Font and
FontMetrics classes?
Ans : The FontMetrics class is used to define
implementation-specific properties, such as ascent
and descent, of a Font object.
Which of the following are passed as an argument
to the paint( ) method?
A Canvas object
A Graphics object
An Image object
A paint object
Ans : b.
Which of the following methods are invoked by the
AWT to support paint and repaint operations?
paint( )
repaint( )
draw( )
redraw( )
Ans : a.
Which of the following classes have a paint( )
method?
Canvas
Image
Frame
Graphics
Ans : a and c.
Which of the following are methods of the Graphics
class?
drawRect( )
drawImage( )
drawPoint( )
drawString( )
Ans : a, b and d.
Which Font attributes are available through the
FontMetrics class?
ascent
leading
case
height
Ans : a, b and d.
Which of the following are true?
The AWT automatically causes a window to be
repainted when a portion of a window has been
minimized and then maximized.
The AWT automatically causes a window to be
repainted when a portion of a window has been
covered and then uncovered.
g.drawString("Dolly",50,10);
}
What will be the result when you attempt to
compile and run the program?
The string "Dolly" will be displayed at the centre of
the frame
b) An error at compilation complaining at the
signature of the paint method
c) The lower part of the word Dolly will be seen at
the top of the form, with the top hidden.
d) The string "Dolly" will be shown at the bottom of
the form
Ans : c.
20) Where g is a graphics instance what will the
following code draw on the screen.
g.fillArc(45,90,50,50,90,180);
a) An arc bounded by a box of height 45, width 90
with a centre point of 50,50, starting
Ans : c.
The CheckboxGroup class is a subclass of the
Component class.
True
False
Ans : b.
5) What are the immediate super classes of the
following classes?
a) Container class
b) MenuComponent class
c) Dialog class
d) Applet class
e) Menu class
Ans : a) Container - Component
b) MenuComponent - Object
c) Dialog - Window
d) Applet - Panel
e) Menu - MenuItem
6) What are the SubClass of Textcomponent Class?
Ans : TextField and TextArea
7) Which method of the component class is used to
set the position and the size of a component?
Ans : setBounds()
8) Which TextComponent method is used to set a
TextComponent to the read-only state?
Ans : setEditable()
9) How can the Checkbox class be used to create a
radio button?
Ans : By associating Checkbox objects with a
CheckboxGroup.
10) What Checkbox method allows you to tell if a
Checkbox is checked?
Ans : getState()
Button
Label
CheckboxMenuItem
Toolbar
Frame
Ans : a, b and e.
18) Which of the following are direct or indirect
subclasses of Container?
Frame
TextArea
MenuBar
FileDialog
Applet
Ans : a,d and e.
19) Which method is used to set the text of a Label
object?
setText( )
setLabel( )
setTextLabel( )
setLabelText( )
Ans : a.
20) Which constructor creates a TextArea with 10
rows and 20 columns?
new TextArea(10, 20)
new TextArea(20, 10)
new TextArea(new Rows(10), new columns(20))
new TextArea(200)
Ans : a.
(Usage is TextArea(rows, columns)
21) Which of the following creates a List with 5
visible items and multiple selection enabled?
new List(5, true)
new List(true, 5)
new List(5, false)
new List(false,5)
Ans : a.
[Usage is List(rows, multipleMode)]
22) Which are true about the Container class?
The validate( ) method is used to cause a
Container to be laid out and redisplayed.
The add( ) method is used to add a Component to
a Container.
The getBorder( ) method returns information about
a Container’s insets.
The getComponent( ) method is used to access a
Component that is contained in a Container.
Ans : a, b and d.
23) Suppose a Panel is added to a Frame and a
Button is added to the Panel. If the Frame’s font is
set to 12-point TimesRoman, the Panel’s font is set
to 10-point TimesRoman, and the Button’s font is
not set, what font will be used to dispaly the
Button’s label?
12-point TimesRoman
11-point TimesRoman
10-point TimesRoman
9-point TimesRoman
Ans : c.
A Frame’s background color is set to Color.Yellow,
and a Button’s background color is to Color.Blue.
Suppose the Button is added to a Panel, which is
added to the Frame. What background color will be
used with the Panel?
Colr.Yellow
Color.Blue
Color.Green
Color.White
Ans : a.
True
False
Ans : b.
An Applet has its Layout Manager set to the default
of FlowLayout. What code would be the correct to
change to another Layout Manager?
setLayoutManager(new GridLayout());
setLayout(new GridLayout(2,2));
c) setGridLayout(2,2,))
d setBorderLayout();
Ans : b.
55) How do you indicate where a component will
be positioned using Flowlayout?
a) North, South,East,West
b) Assign a row/column grid reference
c) Pass a X/Y percentage parameter to the add
method
d) Do nothing, the FlowLayout will position the
component
Ans :d.
An applet
A menu bar
A menu
Ans : b
64) What is the difference between a MenuItem
and a CheckboxMenuItem?
Ans : The CheckboxMenuItem class extends the
MenuItem class to support a menu item
that may be checked or unchecked.
2) malloc
3) extends
4) FALSE
Answer : 3
What will be the result of compiling the following
code:
public class Test {
public static void main (String args []) {
int age;
age = age + 1;
System.out.println("The age is " + age);
}
}
1) Compiles and runs with no output
2) Compiles and runs printing out The age is 1
3) Compiles but generates a runtime error
4) Does not compile
5) Compiles but generates a compile time error
Answer : 4
Which of these is the correct format to use to
create the literal char value a?
(multiple)
1) 'a'
2) "a"
3) new Character(a)
4) \000a
Answer : 1
What is the legal range of a byte integral type?
1) 0 - 65, 535
2) (-128) - 127
3) (-32,768) - 32,767
4) (-256) - 255
Answer : 2
Which of the following is illegal:
1) int i = 32;
2) float f = 45.0;
3) double d = 45.0;
Answer 2
What will be the result of compiling the following
code:
public class Test {
static int age;
public static void main (String args []) {
age = age + 1;
System.out.println("The age is " + age);
}
}
1) Compiles and runs with no output
2) Compiles and runs printing out The age is 1
3) Compiles but generates a runtime error
4) Does not compile
5) Compiles but generates a compile time error
Answer : 2
Which of the following are correct?
(multiple)
1) 128 >> 1 gives 64
2) 128 >>> 1 gives 64
3) 128 >> 1 gives -64
4) 128 >>> 1 gives -64
Answer : 1
Which of the following return true?
(multiple)
1) "john" == new String("john")
2) "john".equals("john")
3) "john" = "john"
4) "john".equals(new Button("john"))
Answer : 2
} catch (Exception e) {
System.out.print("Exception ");
} finally {
System.out.println("Finally");
}
1) Prints out: Exception Finally
2) Prints out: Finally
3) Prints out: Exception
4) No output
Answer : 1
Which of the following is a legal return type of a
method overloading the following method:
public void add(int a) {...}
1) void
2) int
3) Can be anything
Answer : 3
Which of the following statements is correct for a
method which is overriding the following method:
public void add(int a) {...}
1) the overriding method must return void
2) the overriding method must return int
3) the overriding method can return whatever it
likes
Answer : 1
Given the following classes defined in separate
files, what will be the effect of compiling and
running this class Test?
class Vehicle {
public void drive() {
System.out.println("Vehicle: drive");
}
}
class Car extends Vehicle {
Answer : 2
Which variables can an inner class access from the
class which encapsulates it?
(multiple)
1) All static variables
2) All final variables
3) All instance variables
4) Only final instance variables
5) Only final static variables
Answer : 1,2,3
What class must an inner class extend:
1) The top level class
2) The Object class
3) Any class or interface
4) It must extend an interface
Answer 3
In the following code, which is the earliest
statement, where the object originally held in e,
may be garbage collected:
1. public class Test {
2. public static void main (String args []) {
3. Employee e = new Employee("Bob", 48);
4. e.calculatePay();
5. System.out.println(e.printDetails());
6. e = null;
7. e = new Employee("Denise", 36);
8. e.calculatePay();
9. System.out.println(e.printDetails());
10. }
11. }
1) Line 10
2) Line 11
3) Line 7
4) Line 8
5) Never
Answer : 3
What is the name of the interface that can be used
to define a class that can execute within its own
thread?
1) Runnable
2) Run
3) Threadable
4) Thread
5) Executable
Answer : 1
What is the name of the method used to schedule
a thread for execution?
1) init();
2) start();
3) run();
4) resume();
5) sleep();
Answer : 2
Which methods may cause a thread to stop
executing?
(multiple)
1) sleep();
2) stop();
3) yield();
4) wait();
5) notify();
6) notifyAll()
7) synchronized()
Answer : 1,2,3,4
Which of the following would create a text field
able to display 10 characters (assuming a fixed
size font) displaying the initial string "hello":
1) new TextField("hello", 10);
2) new TextField("hello");
3) new textField(10);
4) new TextField();
Answer : 1
Which of the following methods are defined on the
Graphics class:
(multiple)
1) drawLine(int, int, int, int)
2) drawImage(Image, int, int, ImageObserver)
3) drawString(String, int, int)
4) add(Component);
5) setVisible(boolean);
6) setLayout(Object);
Answer : 1,2,3
Which of the following layout managers honours
the preferred size of a component:
(multiple)
1) CardLayout
2) FlowLayout
3) BorderLayout
4) GridLayout
Answer : 2
Given the following code what is the effect of a
being 5:
public class Test {
public void add(int a) {
loop: for (int i = 1; i < 3; i++){
for (int j = 1; j < 3; j++) {
if (a == 5) {
break loop;
}
System.out.println(i * j);
}
}
}
}
1) Generate a runtime error
2) Throw an ArrayIndexOutOfBoundsException
3) Print the values: 1, 2, 2, 4
4) Produces no output
Answer : 4
What is the effect of issuing a wait() method on an
object
1) If a notify() method has already been sent to
that object then it has no effect
2) The object issuing the call to wait() will halt until
another object sends a notify() or notifyAll()
method
3) An exception will be raised
4) The object issuing the call to wait() will be
automatically synchronized with any other objects
using the receiving object.
Answer : 2
The layout of a container can be altered using
which of the following methods:
(multiple)
1) setLayout(aLayoutManager);
2) addLayout(aLayoutManager);
3) layout(aLayoutManager);
4) setLayoutManager(aLayoutManager);
Answer : 1
Using a FlowLayout manager, which is the correct
way to add elements to a container:
1) add(component);
2) add("Center", component);
3) add(x, y, component);
4) set(component);
Answer : 1
Answer : 2
Which of the following illustrates the correct way to
pass a parameter into an applet:
1) <applet code=Test.class age=33 width=100
height=100>
2) <param name=age value=33>
3) <applet code=Test.class name=age value=33
width=100 height=100>
4) <applet Test 33>
Answer : 2
Which of the following correctly illustrate how an
InputStreamReader can be created:
(multiple)
1) new InputStreamReader(new
FileInputStream("data"));
2) new InputStreamReader(new
FileReader("data"));
3) new InputStreamReader(new
BufferedReader("data"));
4) new InputStreamReader("data");
5) new InputStreamReader(System.in);
Answer : 1,5
What is the permanent effect on the file system of
writing data to a new FileWriter("report"), given
the file report already exists?
1) The data is appended to the file
2) The file is replaced with a new file
3) An exception is raised as the file already exists
4) The data is written to random locations within
the file
Answer : 2
What is the effect of adding the sixth element to a
vector created in the following manner:
new Vector(5, 10);
new Second();
}
}
1) Nothing happens
2) A string is printed to the standard out
3) An instance of the class First is generated
4) An instance of the class Second is created
5) An exception is raised at runtime stating that
there is no null parameter constructor in class
First.
6) The class second will not compile as there is no
null parameter constructor in the class First
Answer : 6
What is the result of executing the following
fragment of code:
boolean flag = false;
if (flag = true) {
System.out.println("true");
} else {
System.out.println("false");
}
1) true is printed to standard out
2) false is printed to standard out
3) An exception is raised
4) Nothing happens
Answer : 1
Consider the following classes. What is the result of
compiling and running this class?
public class Test {
public static void test() {
this.print();
}
public static void print() {
System.out.println("Test");
}
public static void main(String args []) {
test();
}
}
(multiple)
1) The string Test is printed to the standard out.
2) A runtime exception is raised stating that an
object has not been created.
3) Nothing is printed to the standard output.
4) An exception is raised stating that the method
test cannot be found.
5) An exception is raised stating that the variable
this can only be used within an instance.
6) The class fails to compile stating that the
variable this is undefined.
Answer : 6
Examine the following class definition:
public class Test {
public static void test() {
print();
}
public static void print() {
System.out.println("Test");
}
public void print() {
System.out.println("Another Test");
}
}
What is the result of compiling this class:
1) A successful compilation.
2) A warning stating that the class has no main
method.
7. s.trim();
(multiple)
1) The compiler would generate an error for line 1.
2) The compiler would generate an error for line 2.
3) The compiler would generate an error for line 3.
4) The compiler would generate an error for line 4.
5) The compiler would generate an error for line 5.
6) The compiler would generate an error for line 6.
7) The compiler would generate an error for line 7.
Answer : 4,6
What is the result of executing the following Java
class:
import java.awt.*;
public class FrameTest extends Frame {
public FrameTest() {
add (new Button("First"));
add (new Button("Second"));
add (new Button("Third"));
pack();
setVisible(true);
}
public static void main(String args []) {
new FrameTest();
}
}
1) Nothing happens.
2) Three buttons are displayed across a window.
3) A runtime exception is generated (no layout
manager specified).
4) Only the "first" button is displayed.
5) Only the "second" button is displayed.
6) Only the "third" button is displayed.
Answer : 6
System.out.println("Hello");
}
public static void main(String args []) {
StaticTest st1 = new StaticTest();
st1.print();
StaticTest st2 = new StaticTest();
st2.print();
}
}
1) Never.
2) Each time a new instance is created.
3) Once when the class is first loaded into the Java
virtual machine.
4) Only when the static method is called explicitly.
Answer : 3
What is the result of the following program:
public class Test {
public static void main (String args []) {
boolean a = false;
if (a = true)
System.out.println("Hello");
else
System.out.println("Goodbye");
}
}
1) Program produces no output but terminates
correctly.
2) Program does not terminate.
3) Prints out "Hello"
4) Prints out "Goodbye"
Answer : 3
Examine the following code, it includes an inner
class, what is the result:
public final class Test4 {
class Inner {
void test() {
if (Test4.this.flag); {
sample();
}
}
}
private boolean flag = true;
public void sample() {
System.out.println("Sample");
}
public Test4() {
(new Inner()).test();
}
public static void main(String args []) {
new Test4();
}
}
1) Prints out "Sample"
2) Program produces no output but terminates
correctly.
3) Program does not terminate.
4) The program will not compile
Answer : 1
Carefully examine the following class:
public class Test5 {
public static void main (String args []) {
/* This is the start of a comment
if (true) {
Test5 = new test5();
System.out.println("Done the test");
}
/* This is another comment */
System.out.println ("The end");
}
}
1) Prints out "Done the test" and nothing else.
2) Program produces no output but terminates
correctly.
3) Program does not terminate.
4) The program will not compile.
5) The program generates a runtime exception.
6) The program prints out "The end" and nothing
else.
7) The program prints out "Done the test" and "The
end"
Answer : 6
What is the result of compiling and running the
following applet:
import java.applet.Applet;
import java.awt.*;
public class Sample extends Applet {
private String text = "Hello World";
public void init() {
add(new Label(text));
}
public Sample (String string) {
text = string;
}
}
It is accessed form the following HTML page:
<html>
<title>Sample Applet</title>
<body>
<applet code="Sample.class" width=200
height=200></applet>
</body>
</html>
Utility Package
}
}
ANSWER : abcdef B) defabc C) abcabc D) defdef
ANSWER : B) defabc
9) Which of the following may have duplicate
elements?
ANSWER : Collection B) List C) Map D) Set
ANSWER : A and B Neither a Map nor a Set may
have duplicate elements.
10) Can null value be added to a List?
ANSWER : Yes.A Null value may be added to any
List.
11) What is the output of the following prg.
import java.util.*;
class Ques{
public static void main (String args[]) {
HashSet set = new HashSet();
String s1 = "abc";
String s2 = "def";
String s3 = "";
set.add(s1);
set.add(s2);
set.add(s1);
set.add(s2);
Iterator i = set.iterator();
while(i.hasNext())
{
s3 += (String) i.next();
}
System.out.println(s3);
}
}
A) abcdefabcdef B) defabcdefabc C) fedcbafedcba
D) defabc
JDBC
1) What are the steps involved in
establishing a connection?
ANSWER : This involves two steps: (1) loading the
driver and (2) making the connection.
2) How can you load the drivers?
ANSWER : Loading the driver or drivers you want
to use is very simple and involves just one line of
code. If, for example, you want to use the JDBC-
ODBC Bridge driver, the following code will load it:
Eg.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Your driver documentation will give you the class
name to use. For instance, if the class name is
jdbc.DriverXYZ , you would load the driver with the
following line of code:
Eg.
Class.forName("jdbc.DriverXYZ");
3) What Class.forName will do while loading
drivers?
ANSWER : It is used to create an instance of a
driver and register it with the DriverManager.
ResultSet rs = stmt.executeQuery("SELECT
COF_NAME, PRICE FROM COFFEES");
Step2.
String s = rs.getString("COF_NAME");
The method getString is invoked on the ResultSet
object rs , so getString will retrieve (get) the value
stored in the column COF_NAME in the current row
of rs
7) What are the different types of
Statements?
ANSWER : 1.Statement (use createStatement
method) 2. Prepared Statement (Use
prepareStatement method) and 3. Callable
Statement (Use prepareCall)
8) How can you use PreparedStatement?
ANSWER : This special type of statement is
derived from the more general class, Statement.If
you want to execute a Statement object many
times, it will normally reduce execution time to use
a PreparedStatement object instead.
The advantage to this is that in most cases, this
SQL statement will be sent to the DBMS right
away, where it will be compiled. As a result, the
PreparedStatement object contains not just an SQL
statement, but an SQL statement that has been
precompiled. This means that when the
PreparedStatement is executed, the DBMS can just
run the PreparedStatement 's SQL statement
without having to compile it first.
Eg.
PreparedStatement updateSales =
con.prepareStatement("UPDATE COFFEES SET
SALES = ? WHERE COF_NAME LIKE ?");
9) What setAutoCommit does?
}
}
12) How can you Move the Cursor in
Scrollable Result Sets ?
ANSWER : One of the new features in the JDBC
2.0 API is the ability to move a result set's cursor
backward as well as forward. There are also
methods that let you move the cursor to a
particular row and check the position of the cursor.
Eg.
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SEN
SITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet srs = stmt.executeQuery("SELECT
COF_NAME, PRICE FROM COFFEES");
The first argument is one of three constants added
to the ResultSet API to indicate the type of a
ResultSet object: TYPE_FORWARD_ONLY,
TYPE_SCROLL_INSENSITIVE , and
TYPE_SCROLL_SENSITIVE .
The second argument is one of two ResultSet
constants for specifying whether a result set is
read-only or updatable: CONCUR_READ_ONLY and
CONCUR_UPDATABLE . The point to remember
here is that if you specify a type, you must also
specify whether it is read-only or updatable. Also,
you must specify the type first, and because both
parameters are of type int , the compiler will not
complain if you switch the order.
Specifying the constant TYPE_FORWARD_ONLY
creates a nonscrollable result set, that is, one in
which the cursor moves only forward. If you do not
specify any constants for the type and updatability
Networking Concepts
1) The API doesn't list any constructors for
InetAddress- How do I create an InetAddress
instance?
ANSWER : In case of InetAddress the three
methods getLocalHost, getByName, getByAllName
can be used to create instances.
E.g.
InetAddress add1;
InetAddress add2;
try{
add1 = InetAddress.getByName("java.sun.com");
add2 = InetAddress.getByName("199.22.22.22");
}catch(UnknownHostException e){}
2) Is it possible to get the Local host IP?
3) What Is a Socket?
A socket is one end-point of a two-way
communication link between two programs
running on the network. A socket is bound to a port
number so that the TCP layer can identify the
application that data is destined to be sent.Socket
classes are used to represent the connection
between a client program and a server program.
The java.net package provides two classes--Socket
and ServerSocket--which implement the client side
of the connection and the server side of the
connection, respectively.
What information is needed to create a TCP
Socket?
ANSWER : The Local System’s IP Address and Port
Number.
And the Remote System's IPAddress and Port
Number.
Encapsulation :
Encapsulation is the mechanism that binds
together code and the data it manipulates and
keeps both safe from outside interference and
misuse.
Inheritance:
Inheritance is the process by which one object
acquires the properties of another object.
Polymorphism:
Polymorphism is a feature that allows one interface
to be used for a general class of actions. The
specific action is determined by the exact nature of
actions.
Code Blocks:
Two or more statements which is allowed to be
grouped into blocks of code is otherwise called as
Code Blocks.This is done by enclosing the
statements between opening and closing curly
braces.
Floating-point numbers:
Floating-point numbers which is also known as real
numbers, are used when evaluating expressions
that require fractional precision.
Unicode:
Unicode defines a fully international character set
that can represent all of the characters found in all
human languages. It is a unification of dozens of