View Single Post
Go4Expert Founder
2Apr2006,14:34  
shabbir's Avatar
Question Number 1
import java.lang.reflect.Field;
MyClass myClass = new MyClass();
try {
Class cl=Class.forName("MyClass");
Field res=cl.getDeclaredField("count");
res.set(myClass,"5");
} catch(Exception e) {}
What is accomplished by the above code?
Choice 1
The first 5 member variables of myClass are copied to "res".
Choice 2
The value of myClass.count is set to 5.
Choice 3
Object "res" is created with its "count" member set to 5.
Choice 4
5 copies of the myClass object are created.
Choice 5
The number of MyClass fields described in "cl" is set to 5.
---------------------------------------------------------------------
Question Number 2
XXX = new Integer("155").toString();
What datatype or class is XXX in the above code segment?
Choice 1
byte
Choice 2
char[]
Choice 3
StringBuffer
Choice 4
String
Choice 5
long
---------------------------------------------------------------------
Question Number 3
What class can access file attributes, create new files and create directories?
Choice 1
java.io.FileSystem
Choice 2
java.io.RandomAccessFile
Choice 3
java.util.Writer
Choice 4
System or Runtime
Choice 5
java.io.File
---------------------------------------------------------------------
Question Number 4
class A {
int i, j, k;
public A(int ii) { i = ii; }
public A() {
k = 1;
}
}
Referring to the above, what code instantiates an object of class A?
Choice 1
new A(this);
Choice 2
A a = new A(3);
Choice 3
A(3) a;
Choice 4
A a = new A(4,8);
Choice 5
A a = new A(3.3);
---------------------------------------------------------------------
Question Number 5
void printOut( int I ) {
if (I==0) return;
for(int i=I;i>0;i--) {
System.out.println("Line " + i);
}
printOut(I-1);
}
What value should be passed to the method printOut, shown above, so that ten lines will be printed?
Choice 1
2
Choice 2
3
Choice 3
4
Choice 4
5
Choice 5
6
---------------------------------------------------------------------
Question Number 6
class A {
public static void main(String args[]) {
char initial = "c";
switch (initial) {
case "j":
System.out.print("John ");
break;
case "c":
System.out.print("Chuck ");
case "t":
System.out.print("Ty ");
break;
case "s":
System.out.print("Scott ");
default:
System.out.print("All");
}
}
}
What is the output from the code above?
Choice 1
John Chuck Ty Scott All
Choice 2
A compilation error will occur.
Choice 3
Chuck Ty
Choice 4
Ty
Choice 5
Chuck
---------------------------------------------------------------------
Question Number 7
How can you insure compatibility of persistent object formats between different versions of Java?
Choice 1
Override the default ObjectInputStream and ObjectOutputStream.
Choice 2
Implement your own writeObject() and readObject() methods.
Choice 3
Add "transient" variables.
Choice 4
Implement java.io.Serializable.
Choice 5
Make the whole class transient
---------------------------------------------------------------------
Question Number 8
What does it mean if a method is final synchronized?
Choice 1
Methods which are synchronized cannot be final.
Choice 2
This is the same as declaring the method private.
Choice 3
Only one synchronized method can be invoked at a time for the entire class.
Choice 4
The method cannot be overridden and is always threadsafe.
Choice 5
All final variables referenced in the method can be modified by only one thread at a time.
---------------------------------------------------------------------
Question Number 9
class HelloWorld extends java.awt.Canvas {
String str = "Hello World";

public void paint(java.awt.Graphics g) {
java.awt.FontMetrics fm = g.getFontMetrics();
g.setColor(java.awt.Color.black);
java.awt.Dimension d = getSize();
__?__
}
}
Which one of the following code segments can be inserted into the method above to paint "Hello World" centered within the canvas?
Choice 1
g.drawText( str, 0, 0);
Choice 2
g.drawString( str, d.width/2, d.height/2 );
Choice 3
g.drawString( str, d.width/2 - fm.stringWidth(str)/2, d.height/2 -
fm.getHeight()/2);
Choice 4
g.translate( d.width/2 - fm.stringWidth(str), d.height/2 - fm.getHeight());
g.drawString( str, 0, 0);
Choice 5
g.drawText( str, d.width, d.height, Font.CENTERED );
---------------------------------------------------------------------
Question Number 10
String url=new String("http://www.tek.com");
How can you retrieve the content of the above URL?
Choice 1
Socket content = new Socket(new URL(url)).collect();
Choice 2
String content = new URLConnection(url).collect();
Choice 3
Object content = new URL(url).getContent();
Choice 4
Object content = new URLConnection(url).getContent();
Choice 5
String content = new URLHttp(url).getString();
---------------------------------------------------------------------
Question Number 11
What is NOT a typical feature of visual JavaBeans?
Choice 1
Persistence, so a bean can be customized in an application builder, saved, and reloaded later.
Choice 2
Properties, both for customization and for programmatic use.
Choice 3
Customization, so that when using an application builder a developer can customize the appearance and behavior of a bean.
Choice 4
Distributed framework, so the visual component resides on the client, while the logic resides on the server.
Choice 5
Events, so that a simple communication metaphor can be used to connect beans.
---------------------------------------------------------------------
Question Number 12
Which one of the following describes the difference between StringBuffer and String?
Choice 1
StringBuffer is used only to buffer data from an input or output stream.
Choice 2
StringBuffer allows text to be changed after instantiation.
Choice 3
StringBuffer holds zero length Strings.
Choice 4
StringBuffer supports Unicode.
Choice 5
StringBuffer is an array of Strings.
---------------------------------------------------------------------
Question Number 13
public double SquareRoot( double value ) throws ArithmeticException
{
if (value >= 0) return Math.sqrt( value );
else throw new ArithmeticException();
}

public double func(int x) {
double y = (double) x;
y *= -9.0;
try {
y = SquareRoot( y );
}
catch(ArithmeticException e) { y /= 3; }
finally { y += 10; }
return y;
}
Referring to the above, what value is returned when method func(9) is invoked?
Choice 1
-37
Choice 2
-27
Choice 3
-17
Choice 4
9
Choice 5
NaN
---------------------------------------------------------------------
Question Number 14
1: Date myDate = new Date();
2: DateFormat dateFormat = DateFormat.getDateInstance();
3: String myString = dateFormat.format(myDate);
4: System.out.println( "Today is " + myString );
How can you change line 2 above so that the date is displayed in the same format used in China?
Choice 1
java.util.Local locale = java.util.Locale.getLocale( java.util.Locale.CHINA );
DateFormat dateFormat = DateFormat.getDateInstance( locale );
Choice 2
java.util.Local locale = java.util.Locale.getLocale("China");
DateFormat dateFormat = DateFormat.getDateInstance( locale );
Choice 3
DateFormat dateFormat = DateFormat.getDateInstance();
dateFormat.setLocale( java.util.Locale.CHINA );
Choice 4
DateFormat dateFormat = DateFormat.getDateInstance(
DateFormat.SHORT, java.util.Locale.CHINA);
Choice 5
DateFormat.setLocale( java.util.Locale.CHINA );
DateFormat dateFormat = DateFormat.getDateInstance();
---------------------------------------------------------------------
Question Number 15
Which one of the following operators has a higher precedence than the modulus operator, % ?
Choice 1
-
Choice 2
*
Choice 3
+
Choice 4
/
Choice 5
. (dot)
---------------------------------------------------------------------
Question Number 16
double dd;
java.util.Random r = new Random(33);
dd = r.nextGaussian();
Referring to the above, after execution, double dd contains a value that is distributed approximately ________ with a standard deviation of ________ and mean of ________.
Choice 1
uniformly, 0, 33
Choice 2
normally, 1, 0
Choice 3
uniformly, 1, 33
Choice 4
normally, 1, 33
Choice 5
normally, 3, 3
---------------------------------------------------------------------
Question Number 17
How can you give other classes direct access to constant class attributes but avoid thread-unsafe situations?
Choice 1
Declare the attributes as public static final.
Choice 2
Implement the "java.lang.SingleThreaded" interface.
Choice 3
Declare the attributes to be synchronized.
Choice 4
This is not possible.
Choice 5
Declare the attributes as protected.
---------------------------------------------------------------------
Question Number 18
import java.io.*;
import java.net.*;
public class NetClient {
public static void main(String args[]) {
try {
Socket skt = new Socket("host",88);
In the above code, what type of connection is Socket object "skt"?
Choice 1
UDP
Choice 2
Connectionless
Choice 3
FTP
Choice 4
HTTP
Choice 5
Connection-oriented
---------------------------------------------------------------------
Question Number 19
float ff;
int gg = 99;
Referring to the above, what is the correct way to cast an integer to a float?
Choice 1
ff = (float) gg
Choice 2
ff (float) = gg
Choice 3
ff = (float: gg
Choice 4
ff = (float) %% gg
Choice 5
You cannot cast an integer to a float
---------------------------------------------------------------------
Question Number 20
Which one of the following is a reason to double-buffer animations?
Choice 1
To draw each image to the screen twice to eliminate white spots
Choice 2
To prevent "hanging" using a MediaTracker object to ensure all frames are loaded before beginning
Choice 3
To draw the next frame to an off-screen image object before displaying to eliminate flicker
Choice 4
To put multiple frame in the same file and uses cropImage() to select the desired frame
Choice 5
To use a small image as wallpaper and a transparent image as the actual frame
---------------------------------------------------------------------
Question Number 21
Which one of the following is NOT a valid java.lang.String declaration?
Choice 1
String myString = new String("Hello");
Choice 2
char data[] = {'a','b','c'};
String str = new String(data);
Choice 3
String myString = new String(5);
Choice 4
String cde = "cde";
Choice 5
String myString = new String();
---------------------------------------------------------------------
Question Number 22
int count = 0;
while(count++ < X ) {
System.out.println("Line " + count);
}
What value for X above will print EXACTLY 10 lines to standard output?
Choice 1
0
Choice 2
5
Choice 3
9
Choice 4
10
Choice 5
11
---------------------------------------------------------------------
Question Number 23
package mypackage.compute.financial;
Referring to the above, where must Class files belonging to the package be stored?
Choice 1
In any classpath directory
Choice 2
In directory mypackage under the current working directory
Choice 3
In mypackage/compute/financial under any directory in the classpath
Choice 4
In mypackage/compute/classpath under any directory
Choice 5
In any directory defined in the path of the java interpreter
---------------------------------------------------------------------
Question Number 24
A monitor called "m" has two threads waiting with the same priority. One of the threads is "thread3". How can you inform "thread3" so that it alone moves from the Waiting state to the Ready state?
Choice 1
thread3.notify();
Choice 2
m.notify(thread3);
Choice 3
thread3.start();
Choice 4
notify(thread3);
Choice 5
thread3.run()
---------------------------------------------------------------------
Question Number 25
if(check4Biz(storeNum) < 10) {}
Referring to the above, what datatype could be returned by method check4Biz()?
Choice 1
char[]
Choice 2
Boolean
Choice 3
int
Choice 4
String
Choice 5
java.util.Bitset
---------------------------------------------------------------------
Question Number 26
Which one of the following is a limitation of subclassing the Thread class?
Choice 1
You must implement the Threadable interface.
Choice 2
You cannot have any static methods in the class.
Choice 3
You must declare the class final.
Choice 4
You must catch the ThreadDeath exception.
Choice 5
You cannot subclass any other class.
---------------------------------------------------------------------
Question Number 27
String os = System.getProperty("os.name");
When the above code is executed in an untrusted applet, which code segment will the JVM execute internally?
Choice 1
throw new java.lang.SecurityException();
Choice 2
System.checkStackTrace();
Choice 3
return SecurityManager.getProperty("os.name");
Choice 4
SecurityManager().getAccess("Property", "os.name");
Choice 5
System.getSecurityManager().checkPropertyAccess("o s.name");
---------------------------------------------------------------------
Question Number 28
public void createTempFiles(String d) {
File f = new File(d);
f.mkdirs();
// more code here ...
}
What is the result if the createTempFiles statement below is executed on the code above on a Unix platform?
if (System.getSecurityManager() == null)
createTempFiles( "/tmp/myfiles/_3214" );
Choice 1
The file "_3214" is created in the "/tmp/myfiles" directory.
Choice 2
A java.lang.SecurityException is thrown.
Choice 3
The "myfiles" directory is created in the "/tmp" directory.
Choice 4
A java.io.DirectoryNotCreatedException is thrown.
Choice 5
The directory "/tmp/myfiles/_3214" is created if it doesn't already exist.
---------------------------------------------------------------------
Question Number 29
static void printIt( int count ) {
if ((count % 2) == 0)
System.err.println("StdErr: Line " + count + "\n");
else {
System.out.println("StdOut: Line " + count + "\n");
}
if (count == 0) return;
else printIt(count-1);
}

public static void main(String [] args) {
printIt( X );
}
What value for X above will print EXACTLY ten lines to standard output, in the fewest number of recursive calls?
Choice 1
5
Choice 2
9
Choice 3
10
Choice 4
19
Choice 5
20
---------------------------------------------------------------------
Question Number 30
for(int i=0;i<5;i= X ) {
System.out.println("Line " + i);
i++;
}
Referring to the above, what value for X will cause a never-ending loop?
Choice 1
A compiler error occurs
Choice 2
4
Choice 3
5
Choice 4
6
Choice 5
10
---------------------------------------------------------------------
Question Number 31
System.err represents an instance of what class?
Choice 1
java.lang.System
Choice 2
java.io.PrintWriter
Choice 3
java.io.OutputStreamWriter
Choice 4
java.io.PrintStream
Choice 5
java.io.BufferedOutputStream
---------------------------------------------------------------------
Question Number 32
<import java.io.*;

Additional code here

FileInputStream f = new FileInputStream("store");
ObjectInputStream in = new ObjectInputStream(f);
Object obj = in.readObject();
Referring to the above, what additional code will produce the type of object represented by "obj"?
Choice 1
Classloader.getInstance(obj);
Choice 2
obj.getClass().getName();
Choice 3
String name;
String all[] = {"String","StringBuffer","Array", others here};
for(int i=0;i<all.length;i++)
if(name = all[i].equals(Class.instanceOf(all[i]))) break;
Choice 4
obj.toString();
Choice 5
new Class.getName(obj);
---------------------------------------------------------------------
Question Number 33
What code declares a two dimensional array of integers called "intArray"?
Choice 1
int intArray[] = {int intArray[]};
Choice 2
int intArray[][];
Choice 3
two dimensional arrays are not allowed in java
Choice 4
int intArray{int intArray[]};
Choice 5
int **intArray;
---------------------------------------------------------------------
Question Number 34
class PrimeThread extends Thread {
long minPrime;
long result;

PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
// . . .
}
}
Referring to the above, which code segment could create and start PrimeThread?
Choice 1
Thread p = new Thread(new PrimeThread(143));
p.run();
Choice 2
PrimeThread p = new PrimeThread();
p.run();
Choice 3
PrimeThread(143).run();
Choice 4
Runnable r = new Runnable( PrimeThread(143) );
r.start();
Choice 5
(new PrimeThread(143)).start();
---------------------------------------------------------------------
Question Number 35
class MyCanvas extends java.awt.Canvas {
int count = 0;
public MyCanvas() {}
public void paint(Graphics g) {
super.paint(g);
}
public int paint(Graphics g) {
super.paint(g);
return ++count;
}
}
Referring to the above, what is WRONG with class MyCanvas?
Choice 1
All methods must declare return datatypes.
Choice 2
There are multiple methods named paint().
Choice 3
A method cannot have the same name as its class.
Choice 4
Overloaded methods cannot have the same input types with a different return type.
Choice 5
java.awt.Canvas is an interface, so it must be implemented, not extended.
---------------------------------------------------------------------
Question Number 36
Objects can be cast to another class when which one of the following is the case?
Choice 1
Both classes are direct subclasses of the same superclass.
Choice 2
The source class is not abstract or static.
Choice 3
The target class is a subclass of the source class.
Choice 4
Both classes are subclasses of the same abstract superclass.
Choice 5
The target class is a final class.
---------------------------------------------------------------------
Question Number 37
What happens if you load a JDBC driver that is NOT fully compliant with JDBC?
Choice 1
The driver will fail bytecode verification.
Choice 2
The driver would function for applications, but not applets.
Choice 3
The driver may work for some actions and not for others.
Choice 4
The SecurityManager will throw a SecurityException when a connection is attempted.
Choice 5
The DriverManager object will refuse to register it.
---------------------------------------------------------------------
Question Number 38
To sign a .class file, the javakey tool does which one of the following?
Choice 1
Embeds a signature flag in the class file attributes
Choice 2
Places a signature byte[] array at the bottom of the class file
Choice 3
Creates an inner class containing the signature data
Choice 4
Places a signature byte[] array at the top of the class file
Choice 5
Places special signature files in a JAR bundle
---------------------------------------------------------------------
Question Number 39
float f = 5.0f;
float g = 2.0f;
double h;

h = 3+f%g+2;
Referring to the above, what is the expected value for h after execution?
Choice 1
5.2
Choice 2
6
Choice 3
7
Choice 4
7.5
Choice 5
9
---------------------------------------------------------------------
Question Number 40
Connection con = DriverManager.getConnection (
"jdbc:odbc:wombat", "login", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
Integer x = rs.getInt("c");
String s = rs.getString("a");
Float f = rs.getFloat("b");
}
What is WRONG with the code above?
Choice 1
Retrieval of the fields is in the wrong order.
Choice 2
The password must be encrypted before being sent to the DriverManager.
Choice 3
The Driver's URL is in the wrong format.
Choice 4
The ResultSet class returns primitive types for integers and floats.
Choice 5
The SQL statement is invalid.
---------------------------------------------------------------------