Text Box: Share on
Linked in
Copyright © 3S Technologies

Email: info@3s-technologies.com

Best viewed in Internet Explorer ver. 9.0 - 1024 X 768 resolution.

Java Objectives - Part 10

The reason for documenting this topic is to provide important technical questions and answers on Java language.  These questions can appear in an aptitude test, organized by different recruiting IT organizations.


Some questions and answers on AWT: CONTROLS, LAYOUT MANAGERS AND MENUS:


1) Which of the following is a legal way to construct a RandomAccessFile ?

1) RandomAccessFile("data", "r");

2) RandomAccessFile("r", "data");

3) RandomAccessFile("data", "read");

4) RandomAccessFile("read", "data");

Ans: 1


2) Carefully examine the following code, When will the string "Hi there" be printed ?

public class StaticTest {

static {

System.out.println("Hi there");


public void print() {



public static void main(String args []) {

StaticTest st1 = new StaticTest();


StaticTest st2 = new StaticTest();





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.

Ans: 3


3) What is the result of the following program ?

public class Test {

public static void main (String args []) {

boolean a = false;

if (a = true)







1) Program produces no output but terminates correctly.

2) Program does not terminate.

3) Prints out "Hello"

4) Prints out "Goodbye"

Ans: 3


4) 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); {





private boolean flag = true;

public void 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

Ans: 1


5) 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"

Ans: 6


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:


<title>Sample Applet</title>


<applet code="Sample.class" width=200 height=200></applet>




1) Prints "Hello World".

2) Generates a runtime error.

3) Does nothing.

4) Generates a compile time error.

Ans: 2


7) What is the effect of compiling and (if possible) running this class ?

public class Calc {

public static void main (String args []) {

int total = 0;

for (int i = 0, j = 10; total > 30; ++i, --j) {

System.out.println(" i = " + i + " : j = " + j);

total += (i + j);


System.out.println("Total " + total);




1) Produce a runtime error

2) Produce a compile time error

3) Print out "Total 0"

4) Generate the following as output:

i = 0 : j = 10

i = 1 : j = 9

i = 2 : j = 8

Total 30

Ans: 3



Some questions and answers on Java Utility Package:


1) What is the Vector class?

Ans: The Vector class provides the capability to implement a growable array of objects.


2) What is the Set interface?

Ans: The Set interface provides methods for accessing the elements of a finite mathematical set.Sets do not allow duplicate elements.


3) What is Dictionary class?

Ans: The Dictionary class is the abstarct super class of Hashtable and Properties class.Dictionary provides the abstarct functions used to store and retrieve objects by key-value.This class allows any object to be used as a key or value.


4) What is the Hashtable class?

Ans: The Hashtable class implements a hash table data structure. A hash table indexes and stores objects in a dictionary using hash codes as the objects' keys. Hash codes are integer values that identify objects.


5) What is the Properties class?

Ans: The properties class is a subclass of Hashtable that can be read from or written to a stream.It also provides the capability to specify a set of default values to be used if a specified key is not found in the table. We have two methods load() and save().


6) What changes are needed to make the following prg to compile?

import java.util.*;

class Ques{

public static void main (String args[]) {

String s1 = "abc";

String s2 = "def";

Vector v = new Vector();



String s3 = v.elementAt(0) + v.elementAt(1);





a) Declare Ques as public

b) Cast v.elementAt(0) to a String

c) Cast v.elementAt(1) to an Object.

d) Import java.lang

Ans: b


7) What is the output of the prg.

import java.util.*;

class Ques{

public static void main (String args[]) {

String s1 = "abc";

String s2 = "def";

Stack stack = new Stack();




String s3 = (String) stack.pop() + (String) stack.pop() ;


}catch (EmptyStackException ex){}



a) abcdef

b) defabc

c) abcabc

d) defdef

Ans: b) defabc


8) Which of the following may have duplicate elements?

a) Collection

b) List

c) Map

d) Set

Ans: a, b (Neither Map nor Set may have duplicate elements)


9) Can null value be added to a List?

Ans: Yes.A Null value may be added to any List.


10) 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 = "";





Iterator i = set.iterator();



s3 += (String) i.next();






a) abcdefabcdef

b) defabcdefabc

c) fedcbafedcba

d) defabc

Ans: d  (Sets may not have duplicate elements.)


11) Which of the following java.util classes support internationalization?

a) Locale

b) ResourceBundle

c) Country

d) Language

Ans: a, b (Country and Language are not java.util classes.)


12) What is the ResourceBundle?

The ResourceBundle class also supports internationalization.

ResourceBundle subclasses are used to store locale-specific resources that can be loaded by a program to tailor the program's appearence to the paticular locale in which it is being run. Resource Bundles provide the capability to isolate a program's locale-specific resources in a standard and modular manner.


13) How are Observer Interface and Observable class, in java.util package, used?

Ans: Objects that subclass the Observable class maintain a list of Observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.


14) Which java.util classes and interfaces support event handling?

Ans: The EventObject class and the EventListener interface support event processing.


15) Does java provide standard iterator functions for inspecting a collection of objects?

Ans: The Enumeration interface in the java.util package provides a framework for stepping once through a collection of objects. We have two methods in that interface.

public interface Enumeration {

boolean hasMoreElements();

Object nextElement();



16) The Math.random method is too limited for my needs- How can I generate random numbers more flexibly?

Ans: The random method in Math class provide quick, convienient access to random numbers, but more power and flexibility use the Random class in the java.util package.

double doubleval = Math.random();

The Random class provide methods returning float, int, double, and long values.

nextFloat() // type float; 0.0 <= value < 1.0

nextDouble() // type double; 0.0 <= value < 1.0

nextInt() // type int; Integer.MIN_VALUE <= value <= Integer.MAX_VALUE

nextLong() // type long; Long.MIN_VALUE <= value <= Long.MAX_VALUE

nextGaussian() // type double; has Gaussian("normal") distribution with mean 0.0 and standard deviation 1.0)

Eg. Random r = new Random();

float floatval = r.nextFloat();




17)  How can we get all public methods of an object dynamically?

Ans: By using getMethods(). It return an array of method objects corresponding to the public methods of this class.

getFields() returns an array of Filed objects corresponding to the public Fields(variables) of this class.

getConstructors() returns an array of constructor objects corresponding to the public constructors of this class.



Some important definitions:


1) Encapsulation :

Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safe from outside interference and misuse.


2) Inheritance:

Inheritance is the process by which one object acquires the properties of another object.


3) 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.


4) 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.


5) Floating-point numbers:

Floating-point numbers which is also known as real numbers, are used when evaluating expressions that require fractional precision.


6) 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 character sets, such as Latin, Greek, Arabic and many more.


7) Booleans:

Java has a simple type called boolean, for logical values. It can have only on of two possible values, true or false.


8) Casting:

A cast is simply an explicit type conversion. To create a conversion between two incompatible types, you must use a cast.


9) Arrays:

An array is a group of like-typed variables that are referred to by a common name. Arrays offer a convenient means of grouping related information. Arrays of any type can be created and may have one or more dimension.


10) Relational Operators:

The relational operators determine the relationship that one operand has to the other. They determine the equality and ordering.


11) Short-Circuit Logical Operators:

The secondary versions of the Boolean AND and OR operators are known as short-

circuit logical operators. It is represented by || and &&.


12) Switch:

The switch statement is Java’s multi-way branch statement. It provides an easy way to

dispatch execution to different parts of your code based on the value of an expression.


13) Jump Statements:

Jump statements are the statements which transfer control to another part of your program. Java Supports three jump statements: break, continue, and return.


14) Instance Variables:

The data, or variable, defined within a class are called instance variable.








3S Technologies

Online Education for all