22 . There are two computers are connected to internet, one computer is trying to open a socket connection to read the home page of another computer, what are the possible exceptions thrown while connection and reading InputStream?

A File class has a method mkdirs() to create directory and its parent directories

A File cannot be used to create directories

36 . Using File class, you can navigate the different directories and list all the files in the those directories?

True

False

37 . Select all the constructor definitions of “FileOutputStream”?

FileOutputStream(FileDescriptor fd)

FileOutputStream(String fileName, boolean append)

FileOutputStream(RandomAccessFile raFile)

FileOutputStream( String dirName, String filename)

38 . Select all correct answers for Font class?

new Font ( Font.BOLD, 18, 16)

new Font ( Font.SERIF, 24, 18)

new Font ( “Serif”, Font.PLAIN, 24);

new Font ( “SanSerif”, Font.ITALIC, 24);

new Font ( “SanSerif”, Font.BOLD+Font.ITALIC, 24);

39 . In an applet programing the requirement is that, what ever the changes you do in the applets graphics context need to be accumulated to the previous drawn information. Select all the correct code snippets?

public void update ( Graphics g) { paint( g) ; }

public void update ( Graphics g) { update( g) ; }

public void update ( Graphics g) { repaint( g) ; }

public void update ( Graphics g) { print( g) ; }

40 . How can you load the image from the same server where you are loading the applet, select the correct answer form the following?

Answers

1 : 1 is correct. Compile Time error.Explanation:Generally static methods can be overridden by static methods only .. Static methods may not be overridden by non static methods..

2 : 3 is correct. for ( int i = 0; i < 1; i++ ) { System.out.print(args[i]); }Explanation:Answer 3 will cause an “ArrayOutOfBoundsException” if you do not pass the command line arguments to the Java Program. 1 and 2 will work without any problem.

3 : 1 is correct. Compile time error.Explanation:The final variables behaves like constants, so the final variables must be initialized before accessing them. They can be initialized where they are declared or in every “constructor” if the class. ( even if class has one or more constructors defined ).

Explanation:The wait() method of an Object class throws InterruptedException when the thread moving from running state to wait state. If the value of timeout is negative or the value of nanos is not in the range 0-999999 then wait() method throws IllegalArgumentException exception at runtime. If the current thread is not the owner of this object’s monitor then it throws IllegalMonitorStateException exception. Click here for more information from Java Documentation.

7 : 1 is correct. Prints Hello how are youExplanation:Assigning or interchanging the object references does not change the values, but if you change the values through object references , changes the values.

8 : 2 & 3 is correct. 2 . Runtime error 3 . ArrayOutOfBoundsException

Explanation:This piece of code throws an ArrayOutOfBoundsException at runtime . If you modify the code int myArray[] = new int[1]; to int myArray[] = new int[2]; , it prints 1 on the screen. The changes you made on the array subscript seen by the caller.

9 : 1,3 & 5 is correct. 1 . int 100 3 . aString 5 . BooleanExplanation:The byte, strictfp are Java keywords and cannot be defined as identifiers, the a-Big-Integer has “-” which is not a valid identifier. The identifiers must starts with letters, $, or _ ( underscore), subsequent characters may be letters, dollar signs, underscores or digits, any other combination will gives you the compiler error.

10 : 2 is correct. x = b ? y : z ;Explanation:If b is true the value of x is y, else the value is z. This is “ternary” operator provides the way to simple conditions into a single expression. If the b is true, the left of ( : ) is assigned to x else right of the ( : ) is assigned to x. Both side of the ( : ) must have the data type.

11 : 1,2 & 3 is correct. 1 . int a [][] = new int [20][20]; 2 . int [] a [] = new int [20][]; 3 . int [][] a = new int [10][];Explanation:Multidimensional arrays in Java are just arrays within arrays. Multidimensional arrays are defined as rows and columns. The outer array must be initialized. If you look at the answers the outer arrays are initialized.

12 : 2 is correct. Compiler error , you cannot assign a value to final variableExplanation:In Java final variables are treated as constants ( comparing to other languages like Visual Basic and etc.) , once it is initialized you cannot change the values of primitive, if final variables are object references then you cannot assign any other references.

13 : 2 & 5 is correct. 2 . Contains any number of non-public classes and only one public class 5 . Package statements should appear only in the first line or before any import statements of source file

Explanation:The source files always contains only one package statement, you cannot define multiple package statements and these statements must be before the import statements. At any point of time Java source files can have any number of non-public class definitions and only one public definition class. If you have any import statements those should be defined before class definition and after the package definition.

14 : 4 is correct. a = 1, b = -1Explanation:The operator >>> is unsigned right shift, the new bits are set to zero, so the -1 is shifted 31 times and became 1 ( because a is defined as integer ). The operator >> is signed right shift operator, the new bits take the value of the MSB ( Most Significant Bit ) . The operator <>> operator. The sifting operation is applicable to only integer data types.

15 : 4 is correct. 4.Explanation:The operator is bitwise XOR operator. The values of a, b, c are first converted to binary equivalents and calculated using ^ operator and the results are converted back to original format.

16 : 3 is correct. Test() method declaration.Explanation:The abstract methods cannot have body. In any class if one method is defined as abstract the class should be defined as abstract class. So in our example the Test() method must be redefined.

17 : 4 is correct. B b = new C(); // Line 4Explanation:According to the inheritance rules, a parent class references can appear to the child class objects in the inheritance chain from top to bottom. But in our example class B, and class C are in the same level of hierarchy and also these two classes does not have parent and child relationship which violates the inheritance rules.

18 : 1 & 2 is correct. 1 . s == s1 2 . s.equals(s1);Explanation:The string objects can be compared for equality using == or the equals() method ( even though these two have different meaning ). In our example the string objects have same wording but both are different in case. So the string object object comparison is case sensitive.

19 : 1,2 & 4 is correct. 1 . It is a blue print 2 . A new data type 4 . To provide multiple inheritanceExplanation:One of the major fundamental change in Java comparing with C++ is interfaces. In Java the interfaces will provide multiple inheritance functionality. In Java always a class can be derived from only one parent, but in C++ a class can derive from multiple parents.

4 . // Comments package com; import java.awt.*; public class MyTest {}Explanation:In a given Java source file, the package statement should be defined before all the import statement or the first line in the .java file provided if you do not have any comments or JavacDoc definitions. The sequence of definitions are:

21 : 4 is correct. Compile time error.Explanation:The code fails at the time Math class instantiation. The java.lang.Math class is final class and the default constructor defined as private. If any class has private constructors , we cannot instantiate them from out the class ( except from another constructor ).

22 : 1 & 2 is correct. 1 . IOException 2 . MalformedURLExceptionExplanation:In Java the the URL class will throw “MalformedURLException while construncting the URL, and while reading incoming stream of data they will throw IOException..

23 : 4 is correct. Compile time error.Explanation:In Java the constructors can throw exceptions. If parent class default constructor is throwing an exception, the derived class default constructor should handle the exception thrown by the parent.

24 : 1 is correct. Executing class A constructor followed by Executing class B constructorExplanation:In Java the constructors can throw exceptions. According to the Java language exceptions, if any piece of code throwing an exception it is callers worry is to handle the exceptions thrown by the piece of code. If parent class default constructor is throwing an exception, the derived class default constructor should handle the exception thrown by the parent. But in our example the non default constructor is throwing an exception if some one calls that constructor they have to handle the exception.

25 : 3 is correct. Compile time error while multiplication.Explanation:This does not compile because according to the arithmetic promotion rules, the * ( multiplication ) represents binary operator. There are four rules apply for binary operators. If one operand is float,double,long then other operand is converted to float,double,long else the both operands are converted to int data type. So in our example we are trying put integer into byte which is illegal.

26 : 1,2 & 3 is correct. 1 . setBounds(), setVisible(), setFont() 2 . add(), remove() 3 . setEnabled(), setVisible()Explanation:The component class is the parent class of all AWT components like Button, List, Label and etc. Using these methods you can set the properties of components. The add(), remove() methods are used to add PopMenu and to remove MenuComponent.

27 : 3 is correct. Frame.Explanation:Java supports two kinds of menus, pull-down and pop-up menus. Pull-down menus are accessed are accessed via a menu bar. Menu bars are only added to Frames.

28 : 1 & 4 is correct. 1 . Frame’s default layout manager is BorderLayout 4 . Canvas has no default behavior or appearanceExplanation:In Java AWT each container has it’s own default layout manager implemented as part of implementation. For example Frame has default layout manager is BorderLayout , Applet has FlowLayout and etc. The Canvas is kind of component where you can draw custom drawings. The Canvas generates Mouse, MouseMotion, and Key events.

30 : 1,3 & 4 is correct. 1 . java.awt.TextArea.SCROLLBARS_NONE 3 . java.awt.TextField generates Key events and Action events 4 . java.awt.TextArea can be scrolled using the keys.Explanation:The TextArea and TextField are the subclasses of TextComponent class. The TextArea has static fields to give you the functionality of horizontal and vertical scroll bars. These are the following fields: java.awt.TextArea.SCROLLBARS_BOTH java.awt.TextArea.SCROLLBARS_NONE java.awt.TextArea.SCROLLBARS_HORIZONTAL_ONLY java.awt.TextArea.SCROLLBARS_VERTICAL_ONLY The TextArea and TextField will generate Key events and TextField will generate Action events apart from the Key events.

31 : 1 is correct. Compile time error.Explanation:In Java there are two types of methods , static and non static methods. Static methods are belong to class and non static methods are belongs to instances. So from a non static method you can call static as well as static methods, but from a static method you cannot call non static methods ( unless create a instance of a class ) but you can call static methods.

32 : 3 is correct. At instantiation of FileReader object.Explanation:While constructing the FileReader object, if the file is not found in the file system the “FileNotFoundException” is thrown. If the input stream is closed before reading the stream throws IOException.

33 : 3 is correct. While writing to the stream f.write(buffer[i]) throws an IOExceptionExplanation:While writing to a IO stream if the stream is closed before writing throws an IOException. In our example the f ( stream ) is closed via f1 reference variable before writing to it.

34 : 4 is correct. While constructing the FileWriter f2 = new FileWriter(“MyFile2.txt”);Explanation:Constructing the FileWriter object, if the file already exists it overrides it (unless explicitly specified to append to the file). FileWriter will create the file before opening it for output when you create the object. In the case of read-only files, if you try to open and IOException will be thrown.

35 : 1,2 & 3 is correct. 1 . A File class can be used to create files and directories 2 . A File class has a method mkdir() to create directories 3 . A File class has a method mkdirs() to create directory and its parent directories.Explanation:The File class has three methods to create empty files, those are createNewFile(), createTempFile(String prefix, String suffix) and createTempFile(String prefix, String suffix, File directory). File class has two utility methods mkdir() and mkdirs() to create directories. The mkdir() method creates the directory and returns either true or false. Returning false indicates that either directory already exists or directory cannot be created because the entire path does not exists. In the situation when the path does not exists use the mkdirs() method to create directory as well as parent directories as necessary.

36 : 1 is correct. True.Explanation:File class can be used to navigate the directories in the underlying file system. But in the File class there is no way you change the directory . Constructing the File class instance will always point to only one particular directory. To go to another directory you may have to create another instance of a File class.

38 : 3,4 & 5 is correct. 3 . new Font ( “Serif”, Font.PLAIN, 24); 4 . new Font ( “SanSerif”, Font.ITALIC, 24); 5 . new Font ( “SanSerif”, Font.BOLD+Font.ITALIC, 24);Explanation:The Font class gives you to set the font of a graphics context. While constructing the Font object you pass font name, style, and size of the font. The font availability is dependent on platform. The Font class has three types of font names called ” Serif”, “SanSerif”, Monospaced” these are called in JDK 1.1 and after “Times Roman”, Helavatica” and “Courier”.

40 : 1 is correct. public void init() { Image i = getImage ( getDocumentBase(), “Logo.jpeg”); }Explanation:The Applet and Toolkit classes has a method getImage() , which has two forms:

getImage(URL file) getImage(URL dir, String file) These are two ways to refer an image in the server . The Applet class getDocumentBase() methods returns the URL object which is your url to the server where you came from or where your image resides.

41 : 4 is correct. Object, Check, ICheck.Explanation:The instanceof operator checks the class of an object at runtime. In our example o refers to Object class and Check and ICheck refers to the subclasses of Object class. Due to the inheritance hierarchy Check and ICheck returns true.

42 : 5 is correct. None of the above.Explanation:There is no way to call a particular thread from a waiting pool. The methods notify() will calls thread from waiting pool, but there is no guaranty which thread is invoked. The method notifyAll() method puts all the waiting threads from the waiting pool in ready state.

43 : 1,3 & 4 is correct. 1 . You can synchronize entire method 3 . Block of code can be synchronized 4 . The notify() and notifyAll() methods are called only within a synchronized codeExplanation:The keyword controls accessing the single resource from multiple threads at the same time. A method or a piece of code can be synchronized, but there is no way to synchronize a calls. To synchronize a method use synchronized keyword before method definition. To synchronize block of code use the synchronized keyword and the arbitrary instance.

44 : 1 is correct. Compile time error.Explanation:The IOException never thrown here. The exception is thrown is InterruptedException. To correct instead of catching IOException use InterruptedException.

45 : 4 is correct. Frame does not visible.Explanation:The Frame is not going to be visible unless you call setVisible(true) method on the Frame’s instance. But the frame instance is available in computers memory. If do not set the size of the Frame you see default size of the frame ( i.e.. in minimized mode)

46 : 3 & 4 is correct. 3 . public abstract void Test(); 4 . native void doSomthing( int i );Explanation:The abstract methods does not have method bodies. In any given class if one method is defined as abstract the class must defined as abstract class.

47 : 1,3 & 4 is correct. 1 . toString() method is defined in Object class. 3 . wait(), notify(), notifyAll() methods are defined in Object class and used for Thread communication. 4 . toString() method provides string representation of an Object state.Explanation:The toString() is defined in Object class the parent all classes which will gives you the string representation of the object’s state. This more useful for debugging purpose. The wait(), notify(), notifyAll() methods are also defined in Object class are very helpful for Thread communication. These methods are called only in synchronized methods.

48 : 1 & 4 is correct. 1 . public transient int val; 4 . synchronized ( this ) { // Assume that “this” is an arbitrary object instance. }Explanation:To define transient variables just include “transient” keyword in the variable definition. The transient variables are not written out any where, this is the way when you do object serialization not to write the critical data to a disk or to a database. The “synchronized” keyword controls the single resource not to access by multiple threads at the same time. The synchronized keyword can be applied to a method or to a block of code by passing the arbitrary object instance name as an argument.

49 : 1,3 & 4 is correct. 1 . Double 3 . Integer 4 . ByteExplanation:In Java all the primitive data types has wrapper classes to represent in object format and will throw “NumberFormatException”. The Boolean does not throw “NumberFormatException” because while constructing the wrapper class for Boolean which accepts string also as an argument.

The method ceil(), returns double value not less than the integer ( in our case 3 ). The other ways to say this method returns max integer value . ( All the decimals are rounded to 1 and is added to integer value ). For negative values it behaves exactly opposite.

The method floor() is exactly reverse process of what ceil() method does.

The round() method just rounds to closest integer value.

51 : 1 is correct. OuterClass.InnerClass inner = new OuterClass().new InnerClass();Explanation:The static methods are class level methods to execute those you do not need a class instance. If you try to execute any non static method or variables from static methods you need to have instance of a class. In our example we need to have OuterClass reference to execute InnerClass method.

52 : 3 is correct. By making s1 as final variable.Explanation:In Java it is possible to declare a class inside a method. If you do this there are certain rules will be applied to access the variables of enclosing class and enclosing methods. The classes defined inside any method can access only final variables of enclosing class.

53 : 3 is correct. This is convention adopted by Sun , to insure that there is no ambiguity between packages and inner classes.Explanation:This is convention adopted to distinguish between packages and inner classes. If you try to use Class.forName() method the call will fail instead use getCLass().getName() on an instance of inner class.

55 : 3 is correct. -1.Explanation:Internally the x value first gets inverted ( two’s compliment ) and then shifted 1 times. First when it is inverted it becomes negative value and shifted by one bit.

56 : 3 is correct. InputEvent.Explanation:The InputEvent class has method getWhen() which returns the time when the event took place and the return type is long.

57 : 2 & 3 is correct. 2 . getSource() method is defined in java.util.EventObject class 3 . getID() method is defined in java.awt.AWTEvent classExplanation:The super class of all event handling is java.util.EventObject which has a method called getSource() , which returns the object that originated the event.

The subclass of EventObject is AWTEvent has a method getID() , which returns the ID of the event which specifies the nature of the event.

58 : 1,2 & 3 is correct. 1 . A listener object is an instance of a class that implements a listener interface. 2 . An event source is an object , which can register listener objects and sends notifications whenever event occurs. 3 . Event sources fires the events.Explanation:The event listeners are instance of the class that implements listener interface . An event source is an instance of class like Button or TextField that can register listener objects and sends notifications whenever event occurs.

59 : 2 & 4 is correct. 2 . As a Queue 4 . As a StackExplanation:This implements java.util.List interface and uses linked list for storage. A linked list allows elements to be added, removed from the collection at any location in the container by ordering the elements. With this implementation you can only access the elements in sequentially.You can easily treat the LinkedList as a stack, queue and etc., by using the LinkedList methods.

60 : 1 is correct. Compilation error.Explanation:The method throwMethod() is throwing and type Exception class instance, while catching the exception you are catching the subclass of Exception class.

File class can be used to navigate the directories in the underlying file system. But in the File class there is no way you change the directory . Constructing the File class instance will always point to only one particular directory. To go to another directory you may have to create another instance of a File class.

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.