0% found this document useful (0 votes)
3 views

java mcqs1-1

The document contains a series of multiple-choice questions related to Java programming concepts, including variable types, method behavior, inheritance, garbage collection, and exception handling. Each question presents a scenario or statement, followed by several options for the correct answer. The questions are designed to test knowledge of Java syntax, object-oriented principles, and programming practices.

Uploaded by

Eranilde Osei
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

java mcqs1-1

The document contains a series of multiple-choice questions related to Java programming concepts, including variable types, method behavior, inheritance, garbage collection, and exception handling. Each question presents a scenario or statement, followed by several options for the correct answer. The questions are designed to test knowledge of Java syntax, object-oriented principles, and programming practices.

Uploaded by

Eranilde Osei
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

1.

The default value of a static integer variable of a class in Java is,


(a) 0 (b) 1 (c) Garbage value (d) Null (e) -1.

2. What will be printed as the output of the following program?


public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println("I = " +i);
}
}
(a) I = 0 (b) I = 1 (c) I = 2 (d) I = 3 (e) Compile-time Error.

3. Multiple inheritance means,


(a) one class inheriting from more super classes
(b) more classes inheriting from one super class
(c) more classes inheriting from more super classes
(d) None of the above
(e) (a) and (b) above.

4. Which statement is not true in java language?


(a) A public member of a class can be accessed in all the packages.
(b) A private member of a class cannot be accessed by the methods of the same class.
(c) A private member of a class cannot be accessed from its derived class.
(d) A protected member of a class can be accessed from its derived class.
(e) None of the above.

5. To prevent any method from overriding, we declare the method as,


(a) static (b) const (c) final (d) abstract (e) none of the above.

6. Which one of the following is not true?


(a) A class containing abstract methods is called an abstract class.
(b) Abstract methods should be implemented in the derived class.
(c) An abstract class cannot have non-abstract methods.
(d) A class must be qualified as ‘abstract’ class, if it contains one abstract method.
(e) None of the above.

7. The fields in an interface are implicitly specified as,


(a) static only (b) protected (c) private
(d) both static and final (e) none of the above.
8. What is the output of the following program:
public class testmeth
{
static int i = 1;
public static void main(String args[])
{
System.out.println(i+” , “);
m(i);
System.out.println(i);
}
public void m(int i)
{
i += 2;
}
}
(a) 1 , 3 (b) 3 , 1 (c) 1 , 1 (d) 1 , 0 (e) none of the
above.
9. Which of the following is not true?
(a) An interface can extend another interface.
(b) A class which is implementing an interface must implement all the methods of the
interface.
(c) An interface can implement another interface.
(d) An interface is a solution for multiple inheritance in java.
(e) None of the above.
10. Which of the following is true?
(a) A finally block is executed before the catch block but after the try block.
(b) A finally block is executed, only after the catch block is executed.
(c) A finally block is executed whether an exception is thrown or not.
(d) A finally block is executed, only if an exception occurs.
(e) None of the above.

11. Among these expressions, which is(are) of type String?


(a) "0" (b) "ab" + "cd"
(c) '0'
(d) Both (A) and (B) above (e) (A), (B) and (C) above.
12. Consider the following code fragment
Rectangle r1 = new Rectangle();
r1.setColor(Color.blue);
Rectangle r2 = r1;
r2.setColor(Color.red);

After the above piece of code is executed, what are the colors of r1 and
r2 (in this order)?
(a) Color.blue
Color.red
(b) Color.blue
Color.blue
(c) Color.red
Color.red
(d) Color.red
Color.blue
(e) None of the above.
13. What is the type and value of the following expression? (Notice the integer division)
-4 + 1/2 + 2*-3 + 5.0
(a) int -5 (b) double -4.5
(c) int -4
(d) double -5.0 (e) None of the above.
14. What is printed by the following statement?
System.out.print("Hello,\nworld!");
(a) Hello, \nworld! (b) Hello, world!
(c)
(d) "Hello, \nworld!" (e) None of the above.
15. Consider the two methods (within the same class)
public static int foo(int a, String s)
{
s = "Yellow";
a=a+2;
return a;
}
public static void bar()
{
int a=3;
String s = "Blue";
a = foo(a,s);
System.out.println("a="+a+" s="+s);
}
public static void main(String args[])
{
bar();
}
What is printed on execution of these methods?
(a) a = 3 s = Blue (b) a = 5 s = Yellow (c) a =
3 s = Yellow
(d) a = 5 s = Blue (e) none of the above.
16. Which of the following variable declaration would NOT compile in a java program?
(a) int var; (b) int VAR; (c) int var1; (d) int var_1; (e) int
1_var;.
17. Consider the following class definition:
public class MyClass
{
private int value;
public void setValue(int i){ /* code */ }
// Other methods...
}
The method setValue assigns the value of i to the instance field value. What could you write
for the implementation of setValue?
(a) value = i; (b) this.value = i; (c) value == i;
(d) Both (A) and (B) and above (e) (A), (B) and (C) above.
18. Which of the following is TRUE?
(a) In java, an instance field declared public generates a compilation error.
(b) int is the name of a class available in the package java.lang
(c) Instance variable names may only contain letters and digits.
(d) A class has always a constructor (possibly automatically supplied by the java
compiler).
(e) The more comments in a program, the faster the program runs.
19. A constructor
(a) Must have the same name as the class it is declared within.
(b) Is used to create objects.
(c) May be declared private
(d) Both (A) and (B) above
(e) (a), (b) and (c) above.
20. Consider,
public class MyClass
{
public MyClass(){/*code*/}
// more code...
}
To instantiate MyClass, you would write?
(a) MyClass mc = new MyClass();
(b) MyClass mc = MyClass();
(c) MyClass mc = MyClass;
(d) MyClass mc = new MyClass;
(e) The constructor of MyClass should be defined as, public void MyClass(){/*code*/}.

21. What is byte code in the context of Java?


(a) The type of code generated by a Java compiler.
(b) The type of code generated by a Java Virtual Machine.
(c) It is another name for a Java source file.
(d) It is the code written within the instance methods of a class.
(e) It is another name for comments written within a program.
22. What is garbage collection in the context of Java?
(a) The operating system periodically deletes all the java files available on the system.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object is
automatically reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that doesn't make
sense.
(e) Janitors working for Sun Micro Systems are required to throw away any Microsoft
documentation found in the employees' offices.
23. You read the following statement in a Java program that compiles and executes.
submarine.dive(depth);
What can you say for sure?
(a) depth must be an int
(b) dive must be a method.
(c) dive must be the name of an instance field.
(d) submarine must be the name of a class
(e) submarine must be a method.
24. The java run time system automatically calls this method while garbage collection.
(a) finalizer() (b) finalize() (c) finally()
(d) finalized() (e) none of the above.
25. The correct order of the declarations in a Java program is,
(a) Package declaration, import statement, class declaration
(b) Import statement, package declaration, class declaration
(c) Import statement, class declaration, package declaration
(d) Class declaration, import statement, package declaration
(e) Class declaration, package declaration, import statement.
26. An overloaded method consists of,
(a) The same method name with different types of parameters
(b) The same method name with different number of parameters
(c) The same method name and same number and type of parameters with different return
type
(d) Both (a) and (b) above
(e) (a), (b) and (c) above.
27. A protected member can be accessed in,
(a) a subclass of the same package (b) a non-subclass of the same package
(c) a non-subclass of different package (d) a subclass of different package
(e) the same class.
Which is the false option?
28. What is the output of the following code:
class eq
{
public static void main(String args[])
{
String s1 = “Hello”;
String s2 = new String(s1);
System.out.println(s1==s2);
}
}
(a) true (b) false (c) 0 (d) 1 (e) Hello.
29. All exception types are subclasses of the built-in class
(a) Exception (b) RuntimeException (c) Error
(d) Throwable (e) None of the above.
30. When an overridden method is called from within a subclass, it will always refer to the version
of that method defined by the
(a) Super class
(b) Subclass
(c) Compiler will choose randomly
(d) Interpreter will choose randomly
(e) None of the abvove.

31.Mark the incorrect statement from the following:


(a) Java is a fully object oriented language with strong support for proper software engineering techniques
(b) In java it is not easy to write C-like so called procedural programs
(c) In java language objects have to be manipulated
(d) In java language error processing is built into the language
(e) Java is not a language for internet programming.
32.In java, objects are passed as
(a) Copy of that object (b) Method called call by value
(c) Memory address (d) Constructor
(e) Default constructor.
33.Which of the following is not a component of Java Integrated Development Environment (IDE)?
(a) Net Beans (b) Borland’s Jbuilder
(c) Symantec’s Visual Café (d) Microsoft Visual Fox Pro
(e) Microsoft Visual J++.
34.Identify, from among the following, the incorrect variable name(s).
(a) _theButton (b) $reallyBigNumber
(c) 2ndName (d) CurrentWeatherStateofplanet
(e) my2ndFont.
Use the following declaration and initialization to evaluate the Java expressions given in questions 5 - 8;
int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;
35.System.out.println( - -b * a + c *d - -);
(a) 21 (b) 24 (c) 28 (d) 26 (e) 22.
36.System.out.println(a++);
(a) 3 (b) 2 (c) 4 (d) 10 (e) Synatax error.
37.System.out.println (–2U * ( g – k ) +c);
(a) 6 (b) 3 (c) 2 (d) 1 (e) Syntax error.
38.System.out.println (c=c++);
(a) 2 (b) 4 (c) 5 (d) 8 (e) Syntax error.
39. Consider the following Java program :
class IfStatement{
public static void main(String args[])
{
int a=2, b=3;
if (a==3)
if (b==3)
System.out.println("===============");
else
System.out.println("#################");
System.out.println("&&&&&&&&&&&");
}
}
Which of the following will the output be?
(a) ===============
(b) #################
&&&&&&&&&
(c) &&&&&&&&&&&
(d) ===============
#################
&&&&&&&&&&
(e) ################.

40. An applet cannot be viewed using


(a) Netscape navigator
(b) Microsoft Internet Explorer
(c) Sun’ Hot Java Browser
(d) Applet viewer tool which comes, with the Java Development Kit.
(e) Jbuilder.
Use the following Java program for answering question 11 and 12
class test{
void meth(int i, int j)
{
i *= 2;
i /= 2;
}
}

class argumentPassing
{
public static void main(String args[])
{
test ob = new test();
int a = 15, b = 20;

System.out.println("a and b before call :"+ a +" " + b);


ob.meth(a,b);
System.out.println("a and b after call : "+ a + " " +b);
}

(Program – III)

What would the output be of the above Program - III before and after it is called?
(a) and b before call : 15 20 a and b after call : 30 10
41. (b) a and b before call : 5 2 a and b after call : 15 20
(c) a and b before call : 15 20 a and b after call : 15 20
(d) a and b before call : 30 10 a and b after call : 15 20
(e) a and b before call : 15 20 a and b after call :
What would the argument passing method be which is used by the above Program - III?
42. (a) Call by value (b) Call by reference
(c) Call by java.lang class (d) Call by byte code
(e) Call by compiler.

Consider the following program:


class prob1{
int puzzel(int n){

int result;

if (n==1)
return 1;
result = puzzel(n-1) * n;
43. return result;
}
}

class prob2{

public static void main(String args[])

prob1 f = new prob1();


System.out.println(" puzzel of 6 is = " + f.puzzel(6));

}
}
Which of the following will be the output of the above program?
(a) 6 (b) 120 (c) 30 (d) 720 (e) 12.
The blank space in the following sentence has to be correctly filled :
44.
Members of a class specified as ……………….. are accessible only to methods of that class.
(a) Protected (b) Final (c) Public (d) Private (e) Static.
Java compiler javac translates Java source code into ………………………
45.(a) Assembler language (b) Byte code
(c) Bit code (d) Machine code
(e) Platform dependent code.
……………….. are used to document a program and improve its readability.
46.
(a) System cells (b) Keywords (c) Comments (d) Control structures (e) Blocks.
In Java, a character constant’s value is its integer value in the ………………………character set.
47.
(a) EBCDIC (b) Unicode (c) ASCII (d) Binary (e) BCD.
In Java, a try block should immediately be followed by one or more ……………….. blocks.
48.
(a) Throw (b) Run (c) Exit (d) Catch (e) Error.
An abstract data type typically comprises a …………… and a set of ……………... respectively.
49.(a) Data representation, classes (b) Database, operations
(c) Data representation, objects (d) Control structure, operations
(e) Data representation, operations.
In object-oriented programming, the process by which one object acquires the properties of another object is called
50.
(a) Encapsulation (b) Polymorphism (c) Overloading
(d) Inheritance (e) Overriding.

Re-implementing an inherited method in a sub class to perform a different task from the parent class is called
(a) Binding (b) Transferring (c) Hiding (d) Coupling (e) extending.

51.
52. In a class definition, the special method provided to be called to create an instance of that class is known as a/an
(a) Interpreter (b) Destructor (c) Constructor (d) Object (e) Compiler.
53. Consider the following statements about Java packages:
I. Packages don’t provide a mechanism to partition all class names into more manageable chunks.
II. Packages provide a visibility control mechanism.
III. One of the important properties of a package is that all classes defined inside a package is accessible by
code outside that package.
IV. The .class files for classes declared to be part of a package can be stored in multiple directories.
Which of them is correct?
(a) Only (I) above (b) Only (II) above
(c) Only (III) above (d) Only (IV) above
(e) All (I), (II), (III) and (IV) above are wrong.
54. Consider the following statements:
I. A class can be declared as both abstract and final.
II. A class declared as final can be extended by defining a sub-class.
III. Resolving calls to methods dynamically at run-time is called late binding.
IV. The class Object defined by Java need not be a super class of all other classes.
Identify the correct statement from the following:
(a) Both (I) and (II) above (b) Both (III) and (IV) above
(c) Both (I) and (III) above (d) Both (II) and (IV) above
(e) Only (III) above.
55. Identify, from among the following, the incorrect descriptions related to Java :
(a) Java Virtual Machine translates byte code into its own system’s machine language and runs the resulting
machine code
(b) The arithmetic operations *, /, %, + and – have the same level of precedence
(c) Comments do not cause any action to be performed during the program execution
(d) All variables must be given a type when they are declared
(e) Java variable names are case-sensitive.
56. Consider the following statement(s) about Java:
I. All white-space characters (blanks) are ignored by the compiler.
II. Java keywords can be used as variable names.
III. An identifier does not begin with a digit and does not contain any spaces.
IV. The execution of Java applications begins at method main.
Which of them is correct?
(a) Both (I) and (III) above (b) Both (II) and (IV) above
(c) Both (I) and (II) above (d) (III) and (IV) above
(e) All (I), (II), (III) and (IV) above.
57. Consider the following data types in Java :
I. Int II. Boolean III. Double IV. String V. Array.
Which of them are simple data types?
(a) Both (I) and (II) above (b) (I), (II), (III) and (IV) above
(c) (I), (II) and (III) above (d) (II) and (III) above
(e) All (I), (II), (III), (IV) and (V) above.
58. For what values respectively of the variables gender and age would the Java expression gender == 1 && age >=
65 become true?
(a) gender = 1, age = 60 (b) gender = 1, age = 50
(c) gender = 1, age = 65 (d) gender = 0, age = 70
(e) gender = 0, age = 55.
59. Consider the following Java program :
public class Compute {

public static void main (string args [ ])


{
int result, x ;
x=1;
result = 0;
while (x < = 10) {
if (x%2 = = 0) result + = x ;
++x;
}
System.out.println(result) ;
}
}
Which of the following will be the output of the above program?
(a) 55 (b) 30 (c) 25 (d) 35 (e) 45.
60. Which of the following statements about Java Threads is correct?
(a) Java threads don’t allow parts of a program to be executed in parallel
(b) Java is a single-threaded language
(c) Java’s garbage collector runs as a high priority thread
(d) Ready, running and sleeping are three states that a thread can be in during its life cycle
(e) Every java application is not multithreaded.
61. A process that involves recognizing and focusing on the important characteristics of a situation or object is
known as:
(a) Encapsulation (b) Polymorphism
(c) Abstraction (d) Inheritance
(e) Object persistence.
62. Which statement is true regarding an object?
(a) An object is what classes instantiated are from
(b) An object is an instance of a class
(c) An object is a variable
(d) An object is a reference to an attribute
(e) An object is not an instance of a class.
63. In object oriented programming, composition relates to
(a) The use of consistent coding conventions
(b) The organization of components interacting to achieve a coherent, common behavior
(c) The use of inheritance to achieve polymorphic behavior
(d) The organization of components interacting not to achieve a coherent common behavior
(e) The use of data hiding to achieve polymorphic behavior.
64. In object oriented programming new classes can be defined by extending existing classes. This is an example
of:
(a) Encapsulation (b) Interface
(c) Composition (d) Inheritance (e) Aggregation.
65. Which of the following does not belong: If a class inherits from some other class, it should

(a) Make use of the parent class's capabilities


(b) Over-ride or add the minimum to accomplish the derived class' purpose
(c) Over-ride all the methods of its parent class
(d) Make sure the result "IS-A-KIND-OF" its base class
(e) Make sure the result “contains” its base class.
66. Object oriented inheritance models the
(a) "is a kind of" relationship
(b) "has a" relationship
(c) "want to be" relationship
(d) inheritance does not describe any kind of relationship between classes
(e) “contains” of relationship.
67. The wrapping up of data and functions into a single unit is called
(a) Encapsulation (b) Abstraction
(c) Data Hiding (d) Polymorphism (e) Message passing.
68. Polymorphism
(a) Is not supported by Java
(b) Refers to the ability of two or more objects belonging to different classes to respond to exactly the same
message in different class-specific ways
(c) Simplifies code maintenance
(d) Not simplifies code manintenance
(e) Refers to the ability of two or more objects belonging to different classes to respond to exactly the
same message in different class –specific ways and simplifies code maintenance.
69. In object oriented programming new classes can be defined by extending existing classes. This is an example
of:
(a) Encapsulation (b) Interface (c) Composition
(d) Inheritance (e) Aggregation.
70. Given a class named student, which of the following is a valid constructor declaration for the class?
(a) Student (student s) { } (b) Student student ( ) { }
(c) Private final student ( ) { } (d) Void student ( ) { }
(e) Static void student(){ }.

71. What is garbage collection in the context of Java?


(a) The operating system periodically deletes all of the java files available on the system.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object is automatically reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that doesn't make sense.
(e) When all references to an object are gone the memory used by the object is not reclaimed.

The concept of multiple inheritance is implemented in Java by


I. Extending two or more classes.
II. Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.
72.
(a) Only (II) (b) (I) and (II) (c) (II) and (III)
(d) Only (I) (e) Only (III).

In Java, declaring a class abstract is useful


(a) To prevent developers from further extending the class
(b) When it doesn't make sense to have objects of that class
73. (c) When default implementations of some methods are not desirable
(d) To force developers to extend the class not to use its capabilities
(e) When it makes sense to have objects of that class.

What is the error in the following class definitions?


Abstract class xy
{
abstract sum (int x, int y) { }
}
74. (a) Class header is not defined properly.
(b) Constructor is not defined.
(c) Method is not defined properly
(d) Method is defined properly
(e) No error.

Which of these field declarations are legal within the body of an interface?
(a) Private final static int answer = 42 (b) public static int answer=42
(c) final static answer =42 (d) int answer
75.
(e) No error.

A package is a collection of
(a) Classes (b) Interfaces (c) Editing tools
76.
(d) Classes and interfaces (e) Editing tools and interfaces.

A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one
of the following is used to enforce such restriction?
(a) Declare the method with the keyword public
(b) Declare the method with the keyword private
77.
(c) Declare the method with the keyword protected
(d) Do not declare the method with any accessibility modifiers
(e) Declare the method with the keyword public and private.

Basic Java language functions are stored in which of the following java package?
78. (a) java.lang (b) java.io (c) java.net (d) java.util (e) java.awt.

Which of the following is a member of the java.lang package?


79.
(a) List (b) Queue (c) Math (d) Stack (e) Process.

Which of the following has a method names flush( )?


80.
(a) Input stream (b) Output Stream
(c) Reader stream (d) Input reader stream
(e) Input output stream.

81. What is the fundamental unit of information of writer streams?


(a) Characters (b) Bytes
(c) Files (d) Records (e) Information.
82. File class is included in which package?
(a) java.io package (b) java.lang package
(c) java.awt package (d) java.net.package
(e) java.util.package.
83. Given the code
String s1 = “ yes” ;
String s2 = “ yes “ ;
String s3 = new String ( s1);
Which of the following would equate to true?

(a) s1 == s2 (b) s1 = s2 (c) s3 == s1 (d) s3=s1 (e) s1!=s2.


84. URL stands for
(a) Universal reader locator (b) Universal reform loader
(c) Uniform resource loader (d) Uniform resource locator
(e) Uniform reader locator.
85. What is the sequence of major events in the life of an applet?
(a) init, start, stop, destroy (b) start, init , stop , destroy
(c) init, start , destroy, stop (d) init, start, destroy
(e) destroy, start, init, stop.
86. Which of the following is true in regard to applet execution?
(a) Applets loaded from the same computer where they are executing have the same restrictions as applets loaded from
the network.
(b) Applets loaded and executing locally have none of the restrictions faced by applets that get loaded from the
network.
(c) Applets loaded and executing locally have some restrictions faced by applets that get loaded from the network.
(d) Applets cant react to user input and change dynamically
(e) Applets can be run independently.
87. What is the return type of the method getID() defined in AWTEvent class
(a) Int (b) long (c) Object (d) Component (e) float.
88. Which of the following events will cause a thread to die?
(a) The method sleep( ) is called
(b) The method wait( ) is called
(c) Execution of the start( ) method ends
(d) Execution of the run( ) method ends
(e) Execution of the run() method is called.
89. What will be the result of the expression 13 & 25?
(a) 38 (b) 25 (c) 9 (d) 12 (e) 21.
90. Which of the following statements are true regarding the finalize( ) method?
(a) The finalize ( ) method must be declared with protected accessibility
(b) The compiler will fail to compile the code that explicitly tries to call the finalize( ) method
(c) The body of the finalize ( ) method can only access other objects that are eligible for garbage collection
(d) The finalize ( ) method can be overloaded
(e) The finalize() method cant be overloaded.

91. Which one of these is a valid method declaration?


(a) void method1
(b) void method2()
(c) void method3(void)
(d) method4()
(e) methods(void).
92. Given a class named Book, which one of these is a valid constructor declaration for the class?
(a) Book(Book b) { }
(b) Book Book() { }
(c) private final Book() { }
(d) void Book() { }
(e) abstract Book() { }.
93. What will be the result of attempting to compile the following program?
public class MyClass {
long var;
public void MyClass(long param) { var = param; } //(1)
public static void main(String[] args) {
MyClass a,b;
a = new MyClass(); //(2)
b = new MyClass(5); //(3)
}
}
(a) A compilation error will occur at (1), since constructors cannot specify a return value
(b) A compilation error will occur at (2), since the class does not have a default constructor
(c) A compilation error will occur at (3), since the class does not have a constructor which
takes one argument of type int
(d) The program will compile correctly
(e) The program will compile and execute correctly.
94. Given the following class, which of these is valid way of referring to the class from outside of the package
net.basemaster?
package net.basemaster;
public class Base {
// . . .
}
Select the correct answer.
(a) By simply referring to the class as Base
(b) By simply referring to the class as basemaster.Base
(c) By simply referring to the class as net.basemaster.Base
(d) By simply referring to the class as net.Base
(e) By importing with net.* and referring to the class as basemaster.Base.
95. Which one of the following class definitions is a valid definition of a class that cannot be instantiated?
(a) class Ghost
{
abstract void haunt();
}
(b) abstract class Ghost
{
void haunt();
}
(c) abstract class Ghost
{
void haunt() { };
}
(d) abstract Ghost
{
abstract void haunt();
}
(e) static class Ghost
{
abstract haunt();
}
96. Which one of the following class definitions is a valid definition of a class that cannot be extended?
(a) class Link { }
(b) abstract class Link { }
(c) native class Link { }
(d) static class Link { }
(e) final class Link { }.
97. Given the following definition of a class, which fields are accessible from outside the package
com.corporation.project?
package com.corporation.project;
public class MyClass
{
int i;
public int j;
protected int k;
private int l;
}
Select the correct answer.
(a) Field i is accessible in all classes in other packages
(b) Field j is accessible in all classes in other packages
(c) Field k is accessible in all classes in other packages
(d) Field l is accessible in all classes in other packages
(e) Field l is accessible in subclasses only in other packages.
98. How restrictive is the default accessibility compared to public, protected, and private accessibility?
(a) Less restrictive than public
(b) More restrictive than public, but less restrictive than protected
(c) More restrictive than protected, but less restrictive than private
(d) More restrictive than private
(e) Less restrictive than protected from within a package, and more restrictive than protected from
outside a package.
99. Which statement is true about accessibility of members?
(a) Private members are always accessible from within the same package
(b) Private members can only be accessed by code from within the class of the member
(c) A member with default accessibility can be accessed by any subclass of the class in which it is
defined
(d) Private members cannot be accessed at all
(e) Package/default accessibility for a member can be declared using the keyword default.
100. Which of the following is true about the use of modifiers?
(a) If no accessibility modifier (public, protected, and private) is specified for a member declaration,
the member is only accessible for classes in the package of its class and subclasses of its class
anywhere
(b) You cannot specify accessibility of local variables. They are only accessible within the block in
which they are declared
(c) Subclasses of a class must reside in the same package as the class they extend
(d) Local variables can be declared static
(e) None of the above.

111. Which of the following statements is true?


(a) Inheritance defines a has-a relationship between a superclass and its subclasses
(b) Every java object has a public method named equals
(c) Every java object has a public method named length
(d) A class can extend any number of other classes
(e) All of the above.
112. Which of the following statements is true?
(a) The keyword extends is used to specify that an interface inherits from another interface
(b) The keyword extends is used to specify that a class inherits from an interface
(c) The keyword implements is used to specify that an interface inherits from another interface
(d) The keyword implements is used to specify that a class inherits from another class
(e) None of the above.
113. Which is the first line that will cause compilation to fail in the following program?
class MyClass
{
public static void main(String[] args)
{
MyClass a;
MySubclass b;

a = new MyClass(); //(1)


b = new MySubclass(); //(2)
a = b; //(3)
b = a; //(4)
a = new MySubclass(); //(5)
}
}
class MySubclass extends MyClass
{
}
(a) Line labeled (1)
(b) Line labeled (2)
(c) Line labeled (3)
(d) Line labeled (4)
(e) Line labeled (5).
114. Given the following definitions and reference declarations, which one of the following assignments is legal?
//Definitions:
interface I1{}
interface I2{}
class C1 implements I1 {}
class C2 implements I2 {}
class C3 extends C1 implements I2 {}
//Reference declarations:
//. . . .
C1 obj1;
C2 obj2;
C3 obj3;
//. . . .
(a) obj2 = obj1;
(b) obj3 = obj1;
(c) obj3 = obj2;
(d) I1 a = obj2;
(e) I1 b = obj3;
115. Given the following class definitions and the reference declarations, what can be said about the statement y
= (Sub) x?
// Class definitions:
class Super { }
class Sub extends Super { }

//Reference declarations
// . . .
Super x;
Sub y;
// . . .
(a) Illegal at compile time
(b) Legal at compile time, but might be illegal at runtime
(c) Definitely legal at runtime, but the (Sub) cast is not strictly needed
(d) Definitely legal at runtime, and the (Sub) cast is needed
(e) None of the above.
116. Given three classes A,B,C, where B is a subclass of A and C is a subclass of B, which one of these Boolean
expressions is true when an object denoted by reference has actually been instantiated from class B as
opposed to from A or C?
(a) (o instanceof B) && (!(o instanceof A))
(b) (o instanceof B) && (!(o instanceof C))
(c) !((o instanceof A) || (o instanceof B))
(d) (o instanceof B)
(e) (o instanceof B) && !((o instanceof A) || (o instanceof C)).
117. What will be the result of attempting to compile and run the following program?
public class Polymorphism
{
public static void main(String[] args)
{
A ref1 = new C();
B ref2 = (B) ref1;
System.out.println(ref2.f());
}
}
class A
{
int f() { return 0; }
}
class B extends A
{
int f() { return 1; }
}
class C extends B
{
int f() { return 2; }
}
(a) The program will fail to compile
(b) The program will compile without error, but will throw a ClassCastException when run
(c) The program will compile without error and print 0 when run
(d) The program will compile without error and print 1 when run
(e) The program will compile without error and print 2 when run.
118. Which of the following statements is true?
(a) Non-static member classes must have either default or public accessibility
(b) All nested classes can declare static member classes
(c) Methods in all nested classes can be declared static
(d) All nested classes can be declared static
(e) Static member classes can contain non-static methods.
119. Which statement is true?
(a) Objects can be explicitly destroyed using the keyword delete
(b) An object will be garbage collected immediately after it becomes unreachable
(c) If object obj1 is accessible from object obj2, and object obj2 is accessible from obj1, then obj1
and obj2 are not eligible for garbage collection
(d) Once an object has become eligible for garbage collection, it will remain eligible until it is
destroyed
(e) If object obj1 can access object obj2 that is eligible for garbage collection, then obj1 is
also eligible for garbage collection.
120. Which statement is true?
(a) If an exception is thrown during the execution of the finalize() method of an eligible object, then
the exception is ignored and the object is destroyed
(b) All objects have a finalize() method
(c) Objects can be destroyed by explicitly calling the finalize() method
(d) The finalize() method can be declared with any accessibility
(e) The compiler will fail to compile code that defines an overriding finalize() method that does not
explicitly call the overridden finalize() method from the superclass.

Answers

111.
Answer : (b)
Reason: Inheritance defines an is-a relation. Aggregation defines a has-a relation. The Object class has a public method
named equals, but it does not have any method named length. Since all classes are subclasses of the Object
class, they all inherit the equals() method. Thus, all Java objects have a public method named equals. In Java,
a class can only extend a single superclass.
112.
Answer : (a)
Reason: The keyword implements is used when a class inherits from an interface. The keyword extends is used when
an interface inherits from another interface or a class inherits from another class.
113.
Answer : (d)
Reason: Line (4) will cause a compile-time error since it attempts to assign a reference value of a supertype object to
a reference of a subtype. The type of the source reference value is MyClass and the type of the destination
reference is MySubclass. Lines (1) and (2) will compile since the reference is assigned a reference value of
the same type. Line (3) will also compile since the reference is assigned a reference value of a subtype.
114.
Answer : (e
Reason: Only the assignment I1 b = obj3 is valid. The assignment is allowed since C3 extends C1, which implements
I1. Assignment obj2 = obj1 is not legal since C1 is not a subclass of C2. Assignments obj3 = obj1 and obj3 =
obj2 are not legal since neither C1 nor C2 is a subclass of C3. Assignment I1 a = obj2 is not legal since C2
does not implement I1.
115.
Answer : (b)
Reason: The statement would be legal at compile time, since the reference x might actually refer to an object of the
type Sub. The cast tells the compiler to go ahead and allow the assignment. At runtime, the reference x may
turn out to denote an object of the type Super instead. If this happens, the assignment will be aborted and a
ClassCastException will be thrown.
116.
Answer : (b)
Reason: The expression (o instanceof B) will return true if the object referred to by o is of type B or a subtype of B. the
expression (!(o instanceof C)) will return true unless the object referred to by o is of type C or a subtype of C.
thus, the expression (o instanceof B) && (!(o instanceof C)) will only return true if the object is of type B or a
subtype of B that is nto C or a subtype of C. Given objects of classes A, B, and C, this expression will only
return true for objects of class B.
117.
Answer : (e)
Reason: The program will print 2 when System.out.println(ref2.f()) is executed. The object referenced by ref2 is of class
C, but the reference is of type B. Since B contains a method f(), the method call will be allowed at compile
time. During execution it is determined that the object is of class C, and dynamic method lookup will cause the
overridden method in C to be executed.
118.
Answer : (e)
Reason: Non-static member classes, unlike top-level classes, can have any accessibility modifier. Static member
classes can only be declared in top-level or nested static member classes and interfaces. Only static member
classes can be declared static. Declaring a class static only means that instances of the class are created
without having an outer instance. This has no bearing on whether the members of the class can be static or
not.
119.
Answer : (e)
Reason: An object is only eligible for garbage collection if all remaining references to the object are from other objects
that are also eligible for garbage collection. An object will no necessarily be garbage collected immediately
after it becomes unreachable. However, the object will be eligible for garbage collection.
120.
Answer : (b)
Reason: The object class defines a protected finalize() method. All classes inherit from Object, thus, all objects have a
finalize() method. The finalize() method of an eligible object is called by the garbage collector to allow the
object to do any cleaning up, before the object is destroyed.
121. Which of the following can Java run from a Web browser exclusively?
(a) Applications
(b) Applets
(c) Servlets
(d) Micro Edition programs
(e) All of the above.
122. Which of the following language is Architecture-Neutral?
(a) Java
(b) C++
(c) C
(d) Ada
(e) Pascal.
123. How the main method header is written in Java?
(a) public static void main(string[] args)
(b) public static void Main(String[] args)
(c) public static void main(String[] args)
(d) public static main(String[] args)
(e) public void main(String[] args).
124. What is the extension name of a Java source code file?
(a) .java
(b) .obj
(c) .class
(d) .exe
(e) .javac.
125. Which of the following is not the reserved words in java?
(a) Public
(b) Static
(c) Void
(d) Class
(e) Num.
126. Suppose
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}
What is the printout of the call Print('a', 4)?
(a) Aaaaa
(b) Aaaa
(c) Aaa
(d) Aa
(e) invalid call.
127. Analyze the following code.
public class Test {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
public static double max(int num1, double num2) {
System.out.println("max(int, double) is invoked");
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, int num2) {
System.out.println("max(double, int) is invoked");
if (num1 > num2)
return num1;
else
return num2;
}
}
(a) The program cannot compile because you cannot have the print statement in a non-void
method
(b) The program cannot compile because the compiler cannot determine which max method
should be invoked
(c) The program runs and prints 2 followed by "max(int, double)" is invoked
(d) The program runs and prints 2 followed by "max(double, int)" is invoked
(e) The program runs and prints "max(int, double) is invoked" followed by 2.
128. Analyze the following code.
public class Test {
public static void main(String[] args) {
System.out.println(m(2));
}
public static int m(int num) {
return num;
}
public static void m(int num) {
System.out.println(num);
}
}
(a) The program has a syntax error because the two methods m have the same signature
(b) The program has a syntax error because the second m method is defined, but not invoked in
the main method
(c) The program runs and prints 2 once
(d) The program runs and prints 2 twice
(e) The program runs and prints 2 thrice.
129. What is Math.rint(3.5)?
(a) 3.0
(b) 3
(c) 4
(d) 4.0
(e) 5.0.
130. Analyze the following code:
public class Test {
public static void main(String[] args) {
int[] x = new int[5];
int i;
for (i = 0; i < x.length; i++)
x[i] = i;
System.out.println(x[i]);
}
}
(a) The program displays 0 1 2 3 4
(b) The program displays 4
(c) The program has a runtime error because the last statement in the main method causes
ArrayIndexOutOfBoundsException
(d) The program has syntax error because i is not defined in the last statement in the main method
(e) The program displays 1 2 3 4 5.

Answers

121 Answer : (b)


. Reason : Applets run from a web browser.
122 Answer : (a)
. Reason : Java is architecture neutral.
123 Answer : (c)
. Reason : public static void main(String[] args) the main method header is written in Java
124 Answer : (a)
. Reason : the extension name of a Java source code file .Java
125 Answer : (e)
. Reason : All are the reserved words of java.
126 Answer : (e)
. Reason : I nvalid call because char 'a' cannot be passed to string message
127 Answer : (b)
. Reason : This is known as ambiguous method invocation
128 Answer : (a)
. Reason : You cannot override the methods based on the type returned.
129 Answer : (d)
. Reason : rint returns the nearest even integer as a double since 3.5 is equally close to 3.0 and 4.0.
130 Answer : (c)
. Reason : After the for loop i is 6. x [5] is out of bounds.

131. Analyze the following code:


public class Test {
public static void main(String[] args) {
int[] oldList = {1, 2, 3, 4, 5};
reverse(oldList);
for (int i = 0; i < oldList.length; i++)
System.out.print(oldList[i] + " ");
}
public static void reverse(int[] list) {
int[] newList = new int[list.length];
for (int i = 0; i < list.length; i++)
newList[i] = list[list.length - 1 - i];
list = newList;
}
}
(a) The program displays 1 2 3 4 5
(b) The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException
(c) The program displays 5 4 3 2 1
(d) The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException
(e) The program displays 54321 and doesnot raise an arrayIndexOutOfBoundsException.
132. If n were declared to be an int and were assigned a non-negative value, the statement:
if (n / 10 % 10 == 3) System.out.println("Bingo!");
will display the message if and only if:
(a) n is divisible (divides evenly) by 3
(b) n is divisible (divides evenly) by 30
(c) The units' digit (also known as the 1's place) of n is 3
(d) The tens' digit of n is 3
(e) The remainder when n is dividied by 30 equals 3.
133. The Java program:
public class Practice2 {
public static void main(String args[]) {
for(int row = -2; row <= 2; row++) {
for(int col = -2; col <= 2; col++)
if( col*col < row*row) System.out.print("* ");
else System.out.print(". ");
System.out.println();
}
}
}
will produce which one of the following patterns when it is executed?
(a) .....
..*..
.***.
..*..
.....
(b) .***.
..*..
.....
..*..
.***.
(c) ..*..
.***.
*****
.***.
..*..
(d) **.**
*...*
.....
*...*
**.**
(e) .....
*....
**...
***..
* * * * ..
134. Suppose the following Java statements:
char ch = ' ';
try {
do {
ch = (char) System.in.read();
} while ( ch != 'G');
} catch(Exception i) {
System.out.println(ch);
}
are executed and applied to the following input which is typed on the keyboard:
g MnGyZ
The final value of the variable ch will be:
(a) 'g'
(b) ''
(c) 'n'
(d) 'G'
(e) 'y'.
135. The Java expression:
! ((b != 0) || (c <= 5))
is equivalent to:
(a) (! (b = 0)) && (! (c > 5))
(b) (b == 0) && (c > 5)
(c) (b != 0) && (c <= 5)
(d) ! ((b <> 0) && (c <= 5))
(e) (b == 0) && (c <= 5).
136. Suppose a method signature looks like this:
public static void Check(char c, Funny f) { ... }
Which one of the following calls to method Check() could possibly be syntactically valid?
(a) Check("a", x);
(b) Check(97, x);
(c) Check(sweat(a[3]), x);
(d) Check(char q, Funny p);
(e) Check(Funny p, char q);.
137. A proposed graduated state income tax applies the following rate schedule to net income after
deductions:
0 --- if the net income is negative
2% --- on incomes less than $10,000
$200, plus 4% of excess over $10,000 --- for incomes between $10,000 and $20,000 (inclusive)
$600, plus 6% of excess over $20,000 --- for incomes between $20,000 and $30,000 (inclusive)
$1,200, plus 8% of excess over $30,000 --- for incomes greater than $30,000
You have been given a program that is supposed to calculate the tax on a given income in dollars. Which
one of the following is the best set of test data on which to try out this program?
(a) it makes no difference, any of the following are equally good!
(b) 2000, 4000, 6000, 8000, 10000, 12000, 14000
(c) -10, 75, 10000, 15000, 20000, 25000, 30000, 40000
(d) 0, 10000, 20000, 30000, 40000
(e) -5000, 0, 5000, 15000, 25000, 35000.
138. You may or may not recall that the standard Java computing environment contains a Math class that
includes a method called random() which returns a double as described below:
public class Math {
// The following method returns a double value with a
// positive sign, greater than or equal to zero,
// but less then 1.0, chosen "randomly" with approximately
// uniform distribution from that range.
public static double random() { ... }
...
}
Which one of the following expressions has a value that is equally likely to have any integer value in the
range 3..8?
(a) (6 * (int)Math.random()*100) + 3
(b) (Math.random()*100 / (int) 6) + 3
(c) (3..8 * Math.random()*100)
(d) (int)(Math.random()*100 % 6) + 3
(e) (6 / (int) Math.random()*100) + 3.
139. Which of the following statement is false?
(a) The general approach of writing programs that are easy to modify is the central theme behind
the design methodology called software engineering
(b) Methods in the same class that have the same name but different signatures are called
overloaded methods
(c) An instance of a class is an object in object-oriented programming
(d) When methods are executed, they are loaded onto the program stack in RAM
(e) To access a method of an object we write <method>.<object>.
140. Consider the following statements:
double mint[ ];
mint = new double[10];
Which of the following is an incorrect usage of System.in.read() with this array?
(a) mint[0] = (double) System.in.read();
(b) mint[1%1+1] = System.in.read();
(c) mint[2] = (char) System.in.read();
(d) mint[3] = System.in.read() * 2.5;
(e) mint[0]= System.in.read(double);.

141. The code fragment:


char ch = ' ';
try {
ch = (char) System.in.read();
while( (ch >= 'A') && (ch <= 'Z'))
ch = (char) System.in.read();
} catch (Exception e) {
System.out.println("error found.");
}
can be expressed equivalently as:
(a) char ch = ' ';
try {
do {
ch = (char) System.in.read();
} while ( (ch >= 'A') && (ch <= 'Z'));
} catch (Exception e) {
System.out.println("error found.");
}
(b) char ch = ' ';
try {
do {
ch = (char) System.in.read();
} while ( (ch >= 'A') || (ch <= 'Z'));
} catch (Exception e) {
System.out.println("error found.");
}
(c) char ch = ' ';
try {
do {
ch = (char) System.in.read();
} while ( (ch < 'A') && (ch > 'Z'));
} catch (Exception e) {
System.out.println("error found.");
}
(d) char ch = ' ';
try {
do {
ch = (char) System.in.read();
} while ( (ch < 'A') || (ch > 'Z'));
} catch (Exception e) {
System.out.println("error found.");
}
(e) char ch = ' ';
try {
do {
ch = (char) System.in.read();
} while ( (ch <= 'A') || (ch >= 'Z'));
} catch (Exception e) {
System.out.println("error found.");
}.
142. What will the following program print when it is executed?
public class Practice11 {
public static void main(String args[]) {
int i = 0;
while (i < 3) {
if( i++ == 0 ) System.out.print("Merry");
if( i == 1) System.out.print("Merr");
if( ++i == 2) System.out.print("Mer");
else System.out.print("Oh no!");
}
}
}
(a) Merry
Merr
Mer
(b) MerryMerrMerOh no!
(c) MerrMerOh no!
(d) MerryMerrMerOh no!
MerrMerOh no!
MerOh no!
(e) Merry.
143. Which of the following is true?
(a) Any applet must be an instance of java.awt.Applet.
(b) You must always provide a no-arg constuctor for an applet.
(c) You must always provide a main method for an applet.
(d) You must always override the init method in an applet.
(e) You must always overload the init method in an applet.
144. Which method that executes immediately after the init () method in an applet?
(a) destroy()
(b) start()
(c) stop()
(d) run()
(e) exit().
145. When you run an applet, which of the following is invoked first?
(a) The init method
(b) The applet's default constructor
(c) The stop method
(d) The destroy method
(e) Start method.
146. When you run the following applet from a browser, what is displayed?
import javax.swing.*;
public class Test extends JApplet {
public Test() {
System.out.println("Default constructor is invoked");
}
public void init() {
System.out.println("Init method is invoked");
}
}
(a) Default constructor is invoked, then Init method is invoked
(b) Init method is invoked, then Default constructor is invoked
(c) Default constructor is invoked
Init method is invoked
(d)
(e) Default constructor is invoked twice.
147. What must A method declare to throw?
(a) Unchecked exceptions
(b) Checked exceptions
(c) Error
(d) RuntimeException
(e) Compliation exception.
148. What information may be obtained from a ResultSetMetaData object?
(a) Database URL and product name
(b) JDBC driver name and version
(c) Number of columns in the result set
(d) Number of rows in the result set
(e) Number of tables in the database.
149. Which of the following statements is true?
(a) You may load multiple JDBC drivers in a program.
(b) You may create multiple connections to a database.
(c) You may create multiple statements from one connection.
(d) You can send queries and update statements through a Statement object.
(e) All of the above.
150. Which is a special file that contains information about the files packaged in a JAR file?
(a) Class file
(b) Source file
(c) Text file
(d) Manifest file
(e) Image file.
Answers

141. Answer : (a)


Reason : Know the difference between while() and do--while(). Also, make
sure you know the syntax of do--while().
142. Answer : (b)
Reason : This program exercises your ability to walk through if() statements
as well as post and pre increment. Be sure to carefully walk through the
code and don't forget the while() loop.
143. Answer : (a)
Reason : Any applet must be an instance of java.awt.Applet except this
statement remaining all are false.
144. Answer : (b)
Reason : start() method that executes immediately after the init()
method in an applet
145. Answer : (b)
Reason : When the applet is loaded to the Web browser, the Web browser
creates an instance of the applet by invoking the applet?s default
constructor.
146. Answer : (a)
Reason : When the applet is loaded to the Web browser, the Web browser
first creates an instance of the applet by invoking the applet?s default
constructor, and then invokes the init() method
147. Answer : (b)
Reason : A method must declare to throw checked options.
148. Answer : (c)
Reason : Number of columns in the resultset information may be obtained
from a ResultSetMetaData object.
149. Answer : (e)
Reason : All the given statements are with respect to JDBC.
150. Answer : (d)
Reason : Manifest file is a special file that contains information about the
files packaged in a JAR file
151. Select the correct statement from among the following:
(a) Java language was developed by Micro Soft
(b) Java language was developed in 1990
(c) Java language was written by James Bond
(d) The initial name given for Java is Oak
(e) The project which developed Java is called Grand
project.
152. Consider the following program written in Java:
class Three{
public static void main(String three[])
{
System.out.println("Hello");
}
}
Select from among the following, a suitable name which
can be used as the source file name.

(a) Hello.java (b) three.java (c)


Three.java (d) Three.class (e) three.class.
153. Java Bytecode is
(a) the Java virtual machine version of machine code.
(b) known as the Java interpreter or Java run time.
(c) a set of programming statements entered into a text
editor by a programmer.
(d) similar to machine code which is specific to any
processor.
(e) not an additional layer in-between the source code and
the machine code.
154. Breaking down a large complex procedure into a
number of smaller procedures is referred to as
(a) data structures (b) top down
decomposition.
(c) structured programming. (d) lists.
(e) amateur programming.
155. Consider the following statements on global data:
Global data
(i) can be accessed by a number of procedures.
(ii) is defined within an outer procedure and may then be
shared by the inner procedure.
(iii) encourages the need to pass the data from one
procedure to another.
(iv) causes inconsistencies when shared by procedures.
Choose the incorrect statement from among the
above.
(a) Only (i) is incorrect (b) Both (i) and (ii)
are incorrect
(c) Only (ii) is incorrect (d) Both (iii) and
(iv) are incorrect
(e) Only (iii) is incorrect.
156. Consider the following variable declarations:
int i;
float j;
boolean k;
Which of the following is correct initialization?
(a) i = 1; j = 1.0; k = true; (b) i = 1; j = 1.0f; k
= true;
(c) i = 1; j = 1.0f; k = “true”; (d) i = 1; j = 1.0; k
= “true”;
(e) i = 1; j = 1.0f; k = True;
157. Use the following declarations and initializations to
evaluate the Java expressions:
(Please note that each expression has been tested
separately.)
int i=3, j=7, k=11;
7) j + k % i

(a) 9 (b) 0 (c)


7 (d) 12 (e) 10.
158. Use the following declarations and initializations to
evaluate the Java expressions:
(Please note that each expression has been tested
separately.)
int i=3, j=7, k=11;
++k –i +i + i++

(a) 16 (b) 17 (c)


14 (d) 13 (e) 15.
159. Use the following declarations and initializations to
evaluate the Java expressions:
(Please note that each expression has been tested
separately.)
int i=3, j=7, k=11;
j * k++ /2

(a) 38.5 (b) 42 (c)


38 (d) 42.0 (e) 42.5.
160. Consider the following Java program:
public class MyClass {
private int myVlaue = 5;
public void printMyVlaue() {
System.out.println(myVlaue);
}
public void setMyVlaue(int myVlaue) {
System.out.println(myVlaue);
this.myVlaue = myVlaue;
}
public static void main(String[] args) {
MyClass myClass1 = new MyClass();
myClass1.setMyVlaue(10);
myClass1.printMyVlaue();
}
}
Which of the following will the output be?
(a) 5 10 (b) 10 10
(c) 10 (d)
5 5 (e)
5 15 5

Answers

151. Answer : (d)


Reason : Remaining all are irrelevant as java was developed by sun,1991etc
152. Answer : (c)
Reason : Source files are to stored using .java extension and Class Name would be preferably the
source file name with case sensitivity.
153. Answer : (a)
Reason : The virtual machine version of machine code is java byte code. It is not java interpreter
or set of.
154. Answer : (b)
Reason : According to OOPS it is termed as top down compostion. Remaining all relate to different
concepts
155. Answer : (e)
Reason : All are the properties of global data whereas the third statement is incorrect.
156. Answer : (b)
Reason : is the appropriate form of initialization. remaining all are incorrect.
157. Answer : (a)
Reason : k%i is 2 and j+2=9.
158. Answer : (e)
Reason : Based on the hierarchy of operations (e) is the correct answer.
159. Answer : (c)
Reason : Based on the hierarchy of operations 38 is the correct answer
160. Answer : (b)
Reason : Based on the methods call the out put is 10 10.
161. java server page is an extension of
(a) html (b) xml (c) xhtml (d) sgml (e) java servlet.
162. Consider the following program while noting the missing variable declaration indicated by
?????? :
in the class Tree.
class Tree {
??????
Tree() {
treeNo++;
System.out.println("Tree "+ treeNo + " is created!"); }
}
public class Variable{
public static void main(String[] args) {
Tree t1 = new Tree();
Tree t2 = new Tree();
Tree t3 = new Tree(); }
}
The output of the above program should turn out to be as shown below:
Tree 1 is created!
Tree 2 is created!
Tree 3 is created!
Select from among the following the correct variable declaration required in the class Tree to
get the above output on the console:
(a) int static treeNo = 0; (b) int treeNo; (c) static int treeNo;
(d) static final int treeNo; (e) int treeNo = 0;
163. Consider the following additional driver program:
public class Main {
public static void main(String[] args) {
ClassB b = new ClassB(1);
ClassC c = new ClassC(2);
}
}
Which of the following would the output be?
(a) ClassA() constructor (b) ClassB(int) constructor (c) ClassA() constructor
ClassB(int) constructor ClassA() constructor ClassB(int) constructor
ClassA() constructor ClassC(int) constructor ClassB(int) constructor
ClassB(int) constructor ClassB(int) constructor ClassC(int) constructor
ClassC(int) constructor ClassA() constructor

(d) ClassB(int) constructor (e) ClassB(int) constructor


ClassA() constructor ClassC(int) constructor
ClassC(int) constructor
ClassA() constructor
164. What is the result of the expression 75.22 + “5.2”?
(a) The double value in 80.42 (b) The string in “80.42”.
(c) The string is “75.225.2” (d) The string is “75.222.5”
(e) The long value is 80.
165. Consider the following Java program to answer this Question:
public class Main {
private String s;
Main() {
this.print("Main() constructor");
}
static void print(String s) {
System.out.println(s);
this.s = s;
}
String getLastPrint() {
return s;
}
public static void main(String[] args) {
Main m = new Main();
m.print("Hello");
Main.print("Hello World");
String last = m.getLastPrint();
What would be the output of the above program if it is executed?
(a) Hello (b) Hello World
(c) Hello (d) Hello (e) error
Hello Hello World
Hello World Hello
166. Consider the following program written in Java to answer this Question:
01.class StringEx{
02.public static void main(String args[]) {
03.String s1="University of ICFAI”;
04.String s2="hello";
05.String s3="HELLO”;
06.System.out.println(s1.substring(10,13));
07.System.out.println(s2.equalsIgnoreCase(s3));
08.}
09.}
What would be the output of the statement indicated in line number 06 when the program is
executed?
(a) University of ICFAI (b) error (c) of
(d) Colombo (e) ity.
167. Consider the following program written in Java to answer this Question:
01.class StringEx{
02.public static void main(String args[]) {
03.String s1="University of ICFAI”;
04.String s2="hello";
05.String s3="HELLO”;
06.System.out.println(s1.substring(10,13));
07.System.out.println(s2.equalsIgnoreCase(s3));
08.}
09.}

What would be the output of the statement indicated in line number 07 when the program is
executed?
(a) HELLO (b) error (c) of (d) false (e) true.

168. Consider the following program written in Java:


class User {
private String userName;
private int userType;
public void setUserName(String s) {
this.userName = s;
}
public void setUserType(int t) {
this.userType = t;
}
public String getUserName() {
return userName;
}
public int getUserType() {
return userType;
}
}
public class Main {
public static void main(String[] args) {
User u = new User();
System.out.println(u.getUserName());
System.out.println(u.getUserType());
}
}
Which of the following is the output?
(a) null null (b) null 0 (c) error (d) 0 null (e) 0 0
169. Consider the following segment of a Java program:
int x=1;
switch(x)
{
case 0:System.out.println(x);break;
case 1:System.out.println(x);break;
case 2:System.out.println(x);
default: System.out.println(x);
}
What will the output be when the above segment is executed as a program?
(a) 0 (b) 1 (c) 2 (d) 1 1 (e) error
170. Consider the following program written in Java:
class Iteration{
public static void main(String args[])
{
for(int i=0;i<5;i++)
{
System.out.println(i);
if(i%2==0)
System.out.println('\t');
}
}
}
When the program is executed what would the outcome be?
(a) 0 (b) 0 (c) 01 23 4 (d) 0 (e) 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4
5 5

Answers

161 Answer : (e)


. Reason : Remaining all are mark up languages
162 Answer : (c)
. Reason : the remaining all would not give the required output
163 Answer : (a)
. Reason : After execution the output of the given question is of choice A other are contradictory
164 Answer : (c)
.
Reason : When one oper and is floating value and the other operated is string, the ‘+’ operator acts like concantenation.
So the result would be a string of 75.225.2”.
165 Answer : (e)
. Reason : Not a valid code
166 Answer : (c)
. Reason : The extracted string of 10, 13 is OF
167 Answer : (e)
. Reason : True because equalsignorecase ignores case and returns true
168 Answer : (b)
. Reason : Remaining all are contradictory.
169 Answer : (b)
. Reason : x is taken as 1 and case 1 is executed and comes out of the switch.
170 Answer : (e)
. Reason : In other cases the values are less compared to required output.

1. A remote control that is used to turn on or off some machine is also called a(n) _____.
A) class
B) interface
C) message
D) instance

2. In a student grading system, Student Last Name, Student Address, and Final Course
Grade would all be classified as what?
A) Inheritance
B) Information
C) Encapsulation
D) Polymorphism

3. What part of object-oriented technology defines superclass and subclass relationships?


A) Inheritance
B) Scalability
C) Encapsulation
D) Polymorphism

4. In a student grading system, objects from different classes communicate with each other.
These communications are known as _____.
A) inheritance
B) polymorphism
C) messages
D) concealment

5. When an object has many forms, it has _____.


A) Inheritance
B) Scalability
C) Encapsulation
D) Polymorphism

6. What term is used to describe the internal representation of an object that is hidden from
view outside the object's definition?
A) Encapsulation
B) Expandable
C) Polymorphism
D) Inheritance

7. What programming language model is organized around "objects" rather than "actions"?
A) Java
B) OOB
C) Perl
D) C+

8. What are the instructions called that tell a system what, how, and when to do something?
A) Object-oriented technology approach
B) Object-oriented database
C) Program
D) Database management

9. What common technique attempts to save time and energy by reducing redundant work in
object-oriented programming?
A) Reduce lines of programming
B) Reuse of code
C) Reduce size of systems being developed
D) Merging different systems together

10. What kind of programming language is Java?


A) Object-oriented programming language
B) Relational programming language
C) Sixth-generation programming language
D) Database management programming language

ANSWERS:
1 2 3 4 5 6 7 8 9 10
B B A C D A B C B A

What will be the output of following program?


#include<iostream.h>
void main()
{
float x;
x=(float)9/2;
cout<<x;
}
4.5
4.0
4
5
_____________________________________________________________________________________
The term __________ means the ability to take many forms.

Inheritance
Polymorphism
Member function
Encapsulation
_____________________________________________________________________________________
Runtime polymorphism is achieved by

Friend function
Virtual function
Operator overloading
Function overloading
_____________________________________________________________________________________
Access to private data

Restricted to methods of the same class


Restricted to methods of other classes
Available to methods of the same class and other classes
Not an issue because the program will not compile
_____________________________________________________________________________________
Additional information sent when an exception is thrown may be placed in

The throw keyword


The function that caused the error
The catch block
An object of the exception class
_____________________________________________________________________________________
A static data member is given a value

Within the class definition


Outside the class definition
When the program is exeuted
Never
_____________________________________________________________________________________
What will be the result of the expression 13 & 25?

38
25
9
12
_____________________________________________________________________________________
In a class specifier ,data or function designated private are accessible

To any function in the program


Only if you the password
To member functions of that class
Only to public members of the class
_____________________________________________________________________________________
Which of the statements are true ?
I. Function overloading is done at compile time.
II. Protected members are accessible to the member of derived class.
III. A derived class inherits constructors and destructors.
IV. A friend function can be called like a normal function.
V. Nested class is a derived class.

I, II, III
II, III, V
III, IV, V
I, II, IV
_____________________________________________________________________________________
At which point of time a variable comes into existence in memory is determined by its

Scope
Storage class
Data type
All of the above
_____________________________________________________________________________________
When the compiler cannot differentiate between two overloaded constructors, they are called

Overloaded
Destructed
Ambiguous
Dubious
_____________________________________________________________________________________
The actual source code for implementing a template function is created when

The declaration of function appears.


The function is invoked.
The definition of the function appears.
None of the above.
_____________________________________________________________________________________
Usually a pure virtual function

Has complete function body


Will never be called
Will be called only to delete an object
Is defined only in derived class
_____________________________________________________________________________________
Which of the following is the valid class declaration header for the derived class d with base classes b1 and b2?

class d : public b1, public b2


class d : class b1, class b2
class d : public b1, b2
class d : b1, b2
_____________________________________________________________________________________
The process of extracting the relevant attributes of an object is known as

Polymorphism
Inheritence
Abstraction
Data hiding
_____________________________________________________________________________________
What features make C++ so powerful ?

Easy implementation
Reusing old code
Reusing old code
All of the above
_____________________________________________________________________________________
Which of the following operator can be overloaded through friend function?

->
=
()
*
_____________________________________________________________________________________
The keyword friend does not appear in

The class allowing access to another class


The class desiring access to another class
The private section of a class
The public section of a class
_____________________________________________________________________________________
Exception handling is targeted at

Run-time error
Compile time error
Logical error
All of the above
_____________________________________________________________________________________
Function templates can accept

Any type of parameters


Only one parameter
Only parameters of the basic type
Only parameters of the derived type
_____________________________________________________________________________________
If the variable count exceeds 100, a single statement that prints “Too many” is
if (count<100) cout << “Too many”;
if (count>100) cout >> “Too many”;
if (count>100) cout << “Too many”;
None of these.
_____________________________________________________________________________________
The mechanism that binds code and data together and keeps them secure from outside world is known as

Abstraction
Inheritance
Encapsulation
Polymorphism
_____________________________________________________________________________________
The operator << when overloaded in a class

must be a member function


must be a non member function
can be both (A) & (B) above
cannot be overloaded
_____________________________________________________________________________________
To access the public function fbase() in the base class, a statement in a derived class function fder() uses the
statement.fbase();

fbase();
fder();
base::fbase();
der::fder();
_____________________________________________________________________________________
In which case is it mandatory to provide a destructor in a class?

Almost in every class


Class for which two or more than two objects will be created
Class for which copy constructor is defined
Class whose objects will be created dynamically
_____________________________________________________________________________________
_________ members of a base class are never accessible to a derived class.

Public
Private
Protected
A,B and C
_____________________________________________________________________________________
What is the error in the following code?
class t
{
virtual void print();
}

No error
Function print() should be declared as static.
Function print() should be defined.
Class t should contain data members.
_____________________________________________________________________________________
It is possible to declare as a friend

A member function
A global function
A class
All of the above
_____________________________________________________________________________________
A struct is the same as a class except that

There are no member functions


All members are public
Cannot be used in inheritance hierarchy
It does have a this pointer
_____________________________________________________________________________________
C++ was originally developed by

Clocksin and Melish


Donald E.Knuth
Sir Richard Hadlee
Bjarne Stroustrup
_____________________________________________________________________________________
What is the output of the following code
char symbol[3]={‘a’,‘b’,‘c’};
for (int index=0; index<3; index++)
cout << symbol [index];

abc
“abc”
Abc
‘abc’
_____________________________________________________________________________________
If we create a file by ‘ifstream’, then the default mode of the file is _________

ios :: out
ios :: in
ios :: app
ios :: binary
_____________________________________________________________________________________
The following can be declared as friend in a class

An object
A class
A public data member
A private data member
_____________________________________________________________________________________
The polymorphism can be characterized by the phrase

One interface,multiple methods


Multiple interfaces,one method
One interface,one method
None of the above
_____________________________________________________________________________________
A virtual class is the same as

An abstract class
A class with a virtual function
A base class
None of the above
_____________________________________________________________________________________
Member functions, when defined within the class specification

Are always inline


Are not inline
Are inline by default, unless they are too big or too complicated
Are not inline by default.
_____________________________________________________________________________________
Assume that we have constructor functions for both base class and derived class. Now consider the declaration in main( ).
Base * P = New Derived; in what sequence will the constructor be called ?

Derived class constructor followed by Base class constructor.


Base class constructor followed by derived class constructor.
Base class constructor will not be called.
Base class constructor will not be called.
_____________________________________________________________________________________
The operator that cannot be overloaded is

++
::
~
()
_____________________________________________________________________________________
Which of the following declarations are illegal?

void *ptr;
char *str = “hello”;
char str = “hello”;
const *int p1;
_____________________________________________________________________________________
Identify the operator that is NOT used with pointers

->
&
*
>>
_____________________________________________________________________________________
Which of the following statements is NOT valid about operator overloading?

Only existing operators can be overloaded


Overloaded operator must have at least one operand of its class type
The overloaded operators follow the syntax rules of the original operator
None of the above
_____________________________________________________________________________________
Overloading a postfix increment operator by means of a member function takes

No argument
One argument
Two arguments
Three arguments
_____________________________________________________________________________________
Which of the following will produce a value 10 if x = 9.7?

floor(x)
abs(x)
log(x)
ceil(x)
_____________________________________________________________________________________
Which of the following is not the characteristic of constructor?

They should be declared in the public section.


They do not have return type.
They can not be inherited.
They can be virtual.
_____________________________________________________________________________________
You may override the class access specifiers

Public members
Public and protected members
Any specific class members you choose
No class members
_____________________________________________________________________________________
You separated a derived class name from its access specifier with

A colon
Two colons
Atleast one space
A semi colon
_____________________________________________________________________________________
Consider the following statements:
int x = 22,y=15;
x = (x>y) ? (x+y) : (x-y);
What will be the value of x after executing these statements?
22
37
7
5
_____________________________________________________________________________________
A friend function to a class, C cannot access

Private data members and member functions


Public data members and member functions
Protected data members and member functions
The data members of the derived class of C
_____________________________________________________________________________________
The members of a class by default are

Public
Protected
Private
Mandatory to specify
_____________________________________________________________________________________
If x =5, y =2 then x ^y equals________.
(where ^ is a bitwise XOR operator)

00000111
10000010
10100000
11001000

171.Consider the following Java applet program:


import java.awt.*;
import java.applet.*;
public class Applet1 extends applet {
int i = 1, j = 2, k = 3, l = 4;
boolean b = false;
public void init() {
i = j + 1;
b = true;
k = l - 2;
}
public void start() {
j = i + 2;
}
public void stop() {
j = k + 1;
}
public void destroy() {
l = i + 5;
}
}
Which of the following is the value of the variable i soon after the statement b = true and after pressing
the restart option in the applet viewer?
(a) 1 (b) 2 (c) 3 (d) 4 (e) 5.
172.Consider the following program written in Java:
class For{
public static void main(String args[])
{
for(int i=0; i<=2; System.out.println(i););
}
}
What would the output of the above program be when the program is executed?
(a) 0 (b) 2 (c) error (d) 1 (e) 2
1 1 2 1
2 0
173.Consider the following Java program:
class While{
public static void main(String args[]){
int x=0;
while(x=<3) {
System.out.println(x);
if(x==2)
continue;
x++;
}
}
}
What would be the output of the above program if executed?
(a) 0 (b) 0 (c) 1 (d) error (e) 2
1 1 2
2 3 3
3
174.Consider the following program written in Java:
class Args{
public static void main(String args[]) {
System.out.println(args[0]);
System.out.println(args[1]);
}
}
The above program is supplemented with the following command line assuming that the program has
been compiled without any errors.
java Args args zero one
What would the output be?
(a) Args (b) Args args (c) args (d) args zero (e) zero one
args zero
175.Consider the following program written in Java:
class DoWhile{
public static void main(String args[])
{
int j=0;
do{
int k=7;
System.out.println(k);
k+=7;
j++;
}
while(j<3);
}
}
What would be the output of the program if it is executed?
(a) 7 (b) 7 (c) 7 (d) 7 (e) error
8 7 14 15
9 7 21 22
176.Consider the following Java program:
class Arr1{
public static void main(String args[]) {
String[] Names={Sarma,Devi,Rakesh};
Names[2]=Ravi;
for(int i=0;i<Names.length;i++)
System.out.println(Names[i]);
}
}
What would the output of the above program be if executed?
(a) Sarma (b) Sarma (c) Sarma (d) error (e) Sarma
Devi Devi Devi Ravi Devi
Rakesh Rakesh Ravi Rakesh Ravi
177.Consider the following program written in Java:
class Arr{
public static void main(String args[]){
int arr[]=new int[5];
for(int i=1;i<arr.length;i++)
arr[i]=i;
for(int j=0;j<arr.length;j++)
System.out.println(arr[j]);
}
}
What would the output of the program be if it is executed?
(a) 0 (b) error (c) null (d) 0 (e) 0
1 0 0 0
2 1 1 1
3 2 2 1
4 3 3 2
178.Consider the following program written in Java:
import java.awt.*;
import java.applet.Applet;
public class AwtEx extends Applet{
Checkbox ch1=new Checkbox("Advance Certificate");
Checkbox ch2=new Checkbox("Degree Certificate");
public void init(){ add(ch1); add(ch2);
}
}
After compiling the program, the following set of codes is also written and saved separately by giving the
name Awt.html.
<applet code="AwtEx.class" height=100 width=150> </applet>
What would be the outcome when appletviewer is invoked by providing the file Awt.html?
(a) (b) (c)

(d) (e)
179.JAR File stands for
(a) Java Runtime File (b) Java Archive File
(c) Java Applet File (d) Java Remote Method File
(e) Java applet Restart File.
180.Pick up the correct order of the life cycle of servlet :
i. Look up any HTTP information
 Determine the browser version, host name of client, cookies, etc.
ii. Read any data sent by the user
 Capture data submitted by an HTML form.
iii. Format the Results
 Generate HTML on the fly
iv. Set the Appropriate HTTP headers
 Tell the browser the type of document being returned or set any cookies.
v. Generate the Results
 Connect to databases, connect to legacy applications, etc.
vi. Send the document back to the client
(a) ii,i,v,iii,iv,vi (b) i,ii,iv,iii,v,vi (c) i,ii,iii,v,vi,iv (d) ii,v,i,iv,vi,iii (e) ii,v,i,iii,iv,vi.

Answers

171. Answer : (d)


Reason : All other cases are not valid results.
172. Answer : (c)
Reason : Declaration itself is incorrect.
173. Answer : (d)
Reason : while condition syntax is incorrect.
174. Answer : (c)
Reason : the output is args zero remaining all are contradictory to code
175. Answer : (b)
Reason : very time k is initialized to 7 overwriting incremented values. Remaining all are
incorrect.
176. Answer : (d)
Reason : remaining all are incorrect.
177. Answer : (a)
Reason : Choice A is the correct layout of the code.
178. Answer : (a)
Reason : Choice A is the correct layout of the code.
179. Answer : (b)
Reason : Remaining all JAR stands for Java Achieve, where as others specially different concepts of
Java.
180. Answer : (a)
Reason : The order of life cycle of servlet is choice a and the remaining all are incorrect
181. What is the printout of the following code?
double x = 5.5;
int y = (int)x;
System.out.println("x is " + x + " and y is " + y);
(a) x is 5.0 and y is 6.0
(b) x is 6.0 and y is 6.0
(c) x is 6 and y is 6
(d) x is 5.5 and y is 5.0
(e) x is 5.5 and y is 6.5.
182. Which of the following is the correct expression of character 4?
(a) 4
(b) “4”
(c) ‘\0004’
(d) ‘4’
(e) ‘\4’.
183. How many bytes a Java character uses for storing?
(a) One byte
(b) Two bytes
(c) Three bytes
(d) Four bytes
(e) Five bytes.
184. Which of the following assignment statement(s) is/are correct?
I. char c = 'd';
II. char c = 100;
III. char c = "d";
IV. char c = "100";
(a) Only (I) above
(b) Only (II) above
(c) Both (I) and (II) above
(d) Both (II) and (III) above
(e) Both (III) and (IV) above.
185. What does the expression (int)(76.0252175 * 100) / 100 evaluate to?
(a) 76.02
(b) 76
(c) 76.0252175
(d) 76.03
(e) 76.0352715.
186. Which of the follows JDK command is correct to run a Java application in
ByteCode.class?
(a) java ByteCode
(b) java ByteCode.class
(c) javac ByteCode.java
(d) javac ByteCode
(e) JAVAC ByteCode.
187. Suppose you define a Java class as follows:
public class Test {
}
In order to compile this program, the source code should be stored in a file
named
(a) Test. class
(b) Test.doc
(c) Test.txt
(d) Test. java
(e) Any name with extension .java.
188. Which of the following statement is not true?
(a) A default no-arg constructor is provided automatically if no constructors
are explicitly declared in the class
(b) At least one constructor must always be defined explicitly
(c) Constructors do not have a return type, not even void
(d) Constructors must have the same name as the class itself
(e) Constructors are invoked using the new operator when an object is
created.
189. What is wrong in the following code?
class TempClass {
int i;
public void TempClass(int j) {
int i = j;
}
}

public class C {
public static void main(String[] args) {
TempClass temp = new TempClass(2);
}
}
(a) The program has a compilation error because TempClass does not have
a default constructor
(b) The program has a compilation error because TempClass does not have
a constructor with an int argument
(c) The program compiles fine, but it does not run because class C is not
public
(d) The program compiles and runs fine
(e) All of the above.
190. To declare a constant MAX_LENGTH as a member of the class, you write
(a) Final static MAX_LENGTH = 99.98;
(b) Final static float MAX_LENGTH = 99.98;
(c) Static double MAX_LENGTH = 99.98;
(d) Final double MAX_LENGTH = 99.98;
(e) Final static double MAX_LENGTH = 99.98;.

Answers

181. : (d)
Answer
Reason: The value is x is not changed after the casting.
182. : (d)
Answer
Reason: You have to write '4'.
183. : (b)
Answer
Reason: Java characters use Unicode encoding.
184. : (c)
Answer
Reason: Choice (B) is also correct, because an int value can be implicitly cast into a char variable. The Unicode of the
character is the int value. In this case, the character is d
185. : (b)
Answer
Reason: Your answer D is incorrect
In order to obtain 76.02, you have divide 100.0.
186. : (a)
Answer
Reason: A is the right choice.
187. : (d)
Answer
Reason: Test.java is the name of the source code of the file.
188. : (b)
Answer
Reason: There is no certain rule that at least one constructor must always be defined explicitly.
189. : (b)
Answer
Reason: The program would be fine if the void keyword is removed from public void TempClass(int j).
190. : (e)
Answer
Reason: E is the right way to represent.

191. What is the printout of the second println statement in the main method?
public class Foo {
int i;
static int s;

public static void main(String[] args) {


Foo f1 = new Foo();
System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);
Foo f2 = new Foo();
System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);
Foo f3 = new Foo();
System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);
}

public Foo() {
i++;
s++;
}
}
(a) f2.i is 1 f2.s is 1
(b) f2.i is 1 f2.s is 2
(c) f2.i is 2 f2.s is 2
(d) f2.i is 2 f2.s is 1
(e) f2.i is 0 f2.s is 1.
192. What is the printout of the third println statement in the main method?
public class Foo {
int i;
static int s;

public static void main(String[] args) {


Foo f1 = new Foo();
System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);
Foo f2 = new Foo();
System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);
Foo f3 = new Foo();
System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);
}

public Foo() {
i++;
s++;
}
}
(a) f3.i is 1 f3.s is 1
(b) f3.i is 1 f3.s is 2
(c) f3.i is 1 f3.s is 3
(d) f3.i is 3 f3.s is 1
(e) f3.i is 3 f3.s is 3.
193. What code may be filled in the blank without causing syntax or runtime errors?
public class Test {
java.util.Date date;
public static void main(String[] args) {
Test test = new Test();
System.out.println(_________________);
}
}
(a) Test.date
(b) Date
(c) Test.date.toString()
(d) Date.toString()
(e) Date.test.
194. Analyze the following code and choose the correct answer:
public class Foo {
private int x;
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println(foo.x);
}
}
(a) Since x is private, it cannot be accessed from an object foo
(b) Since x is defined in the class Foo, it can be accessed by any method
inside the class without using an object. You can write the code to
access x without creating an object such as foo in this code
(c) Since x is an instance variable, it cannot be directly used inside a main
method. However, it can be accessed through an object such as foo in
this code
(d) You cannot create a self-referenced object; that is, foo is created inside
the class Foo
(e) Since x is public it cannot be accessed from an object foo.
195. What is the printout for the second statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
(a) k is 0
(b) k is 1
(c) k is 2
(d) k is 3
(e) k is -1.
196. What would be the result while attempting to compile and run the following code?
public class Test {
public static void main(String[] args) {
double[] x = new double[]{1, 2, 3};
System.out.println("Value is " + x[1]);
}
}
(a) The program has a syntax error because the syntax new double[]{1, 2, 3}
is wrong and it should be replaced by {1, 2, 3}.
(b) The program has a syntax error because the syntax new double[]{1, 2, 3}
is wrong and it should be replaced by new double[3]{1, 2, 3};
(c) The program has a syntax error because the syntax new double[]{1, 2, 3}
is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};
(d) The program compiles and runs fine and the output "Value is 1.0" is
printed
(e) The program compiles and runs fine and the output "Value is 2.0" is
printed.
197. What is the output of the following code?
public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
increase(x);
int[] y = {1, 2, 3, 4, 5};
increase(y[0]);
System.out.println(x[0] + " " + y[0]);
}
public static void increase(int[] x) {
for (int i = 0; i < x.length; i++)
x[i]++;
}
public static void increase(int y) {
y++;
}
}
(a) 0 0
(b) 1 1
(c) 22
(d) 2 1
(e) 1 2.
198. Assume int[] scores = {1, 20, 30, 40, 50}, what value does
java.util.Arrays.binarySearch(scores, 3) return?
(a) 0
(b) -1
(c) 1
(d) 2
(e) -2.
199. When you run an applet, which of the following is invoked first?
(a) The init method
(b) The start method
(c) The stop method
(d) The destroy method
(e) The applet's default constructor.
200. Analyze the following code.
1. import java.util.*;
2. public class Test {
3. public static void main(String[] args) {
4. Calendar[] calendars = new Calendar[10];
5. calendars[0] = new Calendar();
6. calendars[1] = new GregorianCalendar();
7. }
8. }
(a) The program has a syntax error on Line 4 because java.util.Calendar is
an abstract class
(b) The program has a syntax error on Line 5 because java.util.Calendar is
an abstract class.
(c) The program has a syntax error on Line 6 because Calendar[1] is not of
a GregorianCalendar type
(d) Both (a) and (b) above
(e) Both (b) and (c) above.

Answers

191. : (b)
Answer
Reason: i is an instance variable and s is static, shared by all objects of the Foo class.
192. : (c)
Answer
Reason: i is an instance variable and s is static, shared by all objects of the Foo class.
193. : (a)
Answer
Reason: b and d cause syntax errors because date is an instance variable and cannot be accessed from static context. c
is wrong because test.date is null, causing NullPointerException.
194. : (c)
Answer
Reason: (A) is incorrect, since x can be accessed by an object of Foo inside the Foo class. (B) is incorrect because x is
non-static, it cannot be accessed in the main method without creating an object. (D) is incorrect, since it is
permissible to create an instance of the class within the class.(E) is incorrect as x is declared as private but not
public.The best choice is (C).
195. : (c)
Answer
Reason: When computing k = i + j; i = 2 and j = 0.
196. : (e)
Answer
Reason: new double[]{1, 2, 3} is correct. This is the syntax. In this question, double[] x = new double[]{1, 2, 3} is equivalent
to double[] x = {1, 2, 3};
197. : (d)
Answer
Reason: Invoking increase(x) passes the reference of the array to the method. Invoking increase(y[0]) passes the value 1
to the method. The value y[0] outside the method is not changed.
198. : (e)
Answer
Reason: The binarySearch method returns the index of the search key if it is contained in the list. Otherwise, it returns
?insertion point - 1. The insertion point is the point at which the key would be inserted into the list. In this case the
insertion point is 1. Note that the array index starts from 0.
199. : (e)
Answer
Reason: When the applet is loaded to the Web browser, the Web browser creates an instance of the applet by invoking
the applets default constructor.
200. : (b)
Answer
Reason: (A) is incorrect since it is OK to use abstract class as data type for arrays. new Calendar[10] does not create
Calendar objects. It just creates an array with 10 elements, each of which can reference to a Calendar object. (B)
is correct since you cannot create an object from an abstract class. (C) is incorrect since it is fine to create a
GregorianCalendar object and assign its reference to a variable of its superclass type.
201. Every event object has the ________ method.
(a) getSource()
(b) getActionCommand()
(c) getTimeStamp()
(d) getWhen()
(e) getKeyChar().
202. Analyze the following code:
public class Test {
public static void main(String[] args) {
String s = new String("Welcome to Java");
Object o = s;
String d = (String)o;
}
}
(a) When assigning s to o in Object o = s, a new object is created
(b) When casting o to s in String d = (String)o, a new object is created
(c) When casting o to s in String d = (String)o, the contents of o is changed
(d) s, o, and d reference the same String object
(e) s,o,and d doesnot reference the same String object.
203. Analyze the following code:
public class Test {
int x;
{ x++; }
}
(a) The program cannot be compiled, because the statement x++ must be placed inside
a method or a constructor
(b) You cannot construct an instance of Test, because it does not have a constructor
(c) When you construct an instance of Test, the value of x becomes 0;
(d) When you construct an instance of Test, the value of x becomes 1;
(e) When you contruct an instance of Test the value of x become -1.
204. The code causes Java to throw
int number = Integer.MAX_VALUE + 1;
(a) RuntimeException
(b) Exception
(c) Error
(d) Throwable.
(e) No exceptions.
205. After Analyzing the following code, which statement is appropriate?
class Test {
public static void main(String[] args)
throws MyException {
System.out.println("Welcome to Java");
}
}

class MyException extends Error {


}
(a) You should not declare a class that extends Error, because Error raises a fatal error
that terminates the program
(b) You cannot declare an exception in the main method
(c) You declared an exception in the main method, but you did not throw it

(d) The program has a compilation error


(e) You can declare a class that extends error.
206. What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);
(a) 10
(b) 11
(c) 12
(d) 13
(e) 45.
207. What will be the value of i for the following loop?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
(a) 9
(b) 10
(c) 11
(d) 12
(e) Undefined.
208. Which of the following provides the naming services for the server to register the object and
for the client to locate the object?
(a) Server object interface
(b) Server implementation
(c) RMI Registry
(d) Server stub
(e) Server Skeleton.
209. Which of the following statement is not defined in the Object class?
(a) sleep(long milliseconds)
(b) wait()
(c) notify()
(d) notifyAll()
(e) toString().
210. Which of the following method is a static in java.lang.Thread?
(a) run()
(b) sleep(long)
(c) start()
(d) join()
(e) setPriority(int).

Answers

201. : (a)
Answer
Reason: Every event object is a subclass of EventObject, which contains the getSource() method.
202. : (d)
Answer
Reason: Casting object reference variable does not affect the contents of the object.
203. : (d)
Answer
Reason: x++ is in an initialization block. It is invoked when any constructor of the class is invoked.
204. : (e)
Answer
Reason: At present, Java does not throw integer overflow exceptions. The future version of Java may fix this problem to
throw an over flow exception.
205. : (a)
Answer
Reason: When an exception of Error type occurs, your program would terminate. Therefore, you should not declare an
exception that extends Error.
206. : (e)
Answer
Reason: Your answer D is incorrect
y should be 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
207. : (e)
Answer
Reason: The scope of i is inside the loop. After the loop, i is not defined.
208. : (c)
Answer
Reason: The RMI registry provides the naming services for the server to register the object and for the client to locate the
object.
209. : (a)
Answer
Reason: Remaining other statements are defined in the object class.
210. : (b)
Answer
Reason: B is the apt choice.

211. Which of the following is/are valid comment(s) in java?


(a) /** comment */
(b) /* comment */
(c) /* comment
(d) // comment
(e) Both (a) and (b).
212. What is the correct signature of the main method?
(a) public static main(String[] args)
(b) public static void main(String[] args)
(c) public static void main(String[] )
(d) public static void main(String args)
(e) public void main(String[] args).
213. Which of the following is not a valid statement?
(a) Instance methods can access instance variables and instance methods directly
(b) Instance methods can access class variables and class methods directly
(c) Class methods can access class variables and class methods directly
(d) Class methods can access instance variables or instance methods directly
(e) Class methods cannot use ‘this’ keyword as there is no instance for this to refer to.
214. What is the correct order of defining a Class?
1. The name of the class's parent (super class), if any, preceded by the keyword extends. A class can
only extend (subclass) one parent.
2. The class name, with the initial letter capitalized by convention.
3. Modifiers such as public, private, and a number of others that you will encounter later.
4. The class body, surrounded by braces, {}.
5. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
(a) 3,1,2,5,4
(b) 1,2,3,5,4
(c) 3,2,1,5,4
(d) 5,1,2,3,4
(e) 5,4,3,2,1.
215. public class PassPrimitiveByValue {
public static void main(String[] args) {
int x = 3;
//invoke passMethod() with x as argument
passMethod(x);
// print x to see if its value has changed
System.out.println("After invoking passMethod, x = " + x);
}
// change parameter in passMethod()
public static void passMethod(int p) {
p = 10;
}
}
What is the output of the above program?
(a) 10
(b) 3
(c) garbage value
(d) 4
(e) 10.3.
216. What is the output of the following code?
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
IdentifyMyParts.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("IdentifyMyParts.x = " + a.x);
System.out.println("b.x = " + b.x);
(a) a.y = 5
b.y = 6
a.x = 2
b.x = 2
(b) a.y = 5
b.y = 5
a.x = 2
b.x = 2
(c) a.y = 5
b.y = 6
a.x = 3
b.x = 2
(d) a.y = 5
b.y = 6
a.x = 2
b.x = 3
(e) a.y = 6
b.y = 6
a.x = 2
b.x = 2.
217. Which of the following is legal valid variable name?
(a) 1num
(b) #twenty
(c) @one
(d) timeOfDay
(e) temp val.
218. What is the bit size of int in java?
(a) 8
(b) 16
(c) 32
(d) 64
(e) 128.
219. Which one of these is a valid constructor declaration for the class named student?
(a) Student(student b) { }
(b) Student Student() { }
(c) private final Student[] { }
(d) void Student() { }
(e) abstract student() { }.
220. Which access specifiers should be implemented in a class for all interface methods?
I. Private.
II. Public.
III. Protected.
(a) Only (I) above
(b) Only (II) above
(c) Only (III) above
(d) Both (I) and (III) above
(e) All (I), (II) and (III) above.

Answers

211. Answer : (c)


Reason: The Java programming language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of
comment, just like it ignores comments that use /* and */.
// text
The compiler ignores everything from // to the end of the line.
Answer
212. : (b)
Reason: The correct signature is public static void main(String[] args)
Answer
213. : (d)
Reason: Class methods cannot access instance variables or instance methods directly—they must use an object
reference.
214. Answer : (c)
Reason: class declarations can include these components, in order:
1. Modifiers such as public, private, and a number of others that you will encounter later.
2. The class name, with the initial letter capitalized by convention.
3. The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only
extend (subclass) one parent.
4. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
The class body, surrounded by braces, {}.
Answer
215. : (b)
Reason: Primitive arguments, such as an int or a double, are passed into methods by value. This means that any
changes to the values of the parameters exist only within the scope of the method. When the method
returns, the parameters are gone and any changes to them are lost.So the output is 3 only.
Answer
216. : (a)
Reason: Remaining all are contradictory.
Answer
217. : (d)
Reason: Can only Start with a Letter, Underscore (_) , or Dollar Sign ($).After the first letter Name can include any
letter or Number but cannot include Symbols such as %, @, * and so on.
Answer
218. : (c)
Reason: The bit size of int is 32.
Answer
219. : (a)
Reason: A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.
Answer
220. : (b)
Reason: Interfaces are abstraction of functionality that can be implemented in more than one way when required.
The abstraction must be made available as public for the use by other parts of the program.

221. Which of the following statements is/are valid array declarations?


I. int number();
II. float average[];
III. double[] marks;
IV. counter int[];
V. []double marks;
(a) Both (II) and (III) above
(b) Only (I) above
(c) Both (I) and (III) above
(d) Only (IV) above
(e) Only (V) above.
222. Consider the following code
int number[] = new int[5]; After execution of this statement, which of the following is/are true?
I. number[0] is undefined.
II. number[5] is undefined.
III. number[4] is null.
IV. number[2] is 0.
V. number.length() is 5.
(a) (II), (IV) and (V) above
(b) Both (I) and (V) above
(c) Both (III) and (V) above
(d) Only (V) above
(e) All (I), (II), (III), (IV) and (V) above.
223. What will be the content of array variable table after executing the following code?
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
if(j == i) table[i][j] = 1;
else table[i][j] = 0;
(a) 100
010
001
(b) 000
000
000
(c) 100
110
111
(d) 001
010
100
(e) 000
001
1 0 0.
224. Which of the following statements about abstract methods/classes in Java is/are true?
I. An abstract class cannot be instantiated.
II. Constructors cannot be abstract.
III. A subclass of an abstract class must defined the abstract methods.
IV. Static methods may be declared abstract.
(a) Both (I) and (II) above
(b) (I), (II) and (III) above
(c) Only (I) above
(d) Only (II) above
(e) All (I), (II), (III) and (IV) above.
225. Which keyword can protect a class in a package from accessibility by the classes outside the package?
(a) don't use any keyword at all (make it default)
(b) Private
(c) Protected
(d) final
(e) static.
226. We would like to make a member of a class visible in all subclasses regardless of what package they are
in. Which one of the following keywords would achieve this?
(a) Private or protected
(b) Private
(c) Protected
(d) Public
(e) Private or public.
227. The type long can be used to store values in the following range:
(a) -263 to 263-1
(b) -231 to 231-1
(c) -264 to 264
(d) -232 to 232-1
(e) 232 to 264-1.
228. Which of the following is/are not hexadecimal numbers?
I. 999.
II. (hex)23.
III. 0x556.
IV. 0x1F2.
(a) Both (I) and (II) above
(b) Only (I) above
(c) Only (III) above
(d) (I), (II) and (III) above
(e) All (I), (II), (III) and (IV) above.
229. Select the invalid assignment statement(s) from the following:
I. Float x = 238.88;
II. Double y = 0x443;
III. int n = (int) true;
IV. long m =778645;
(a) Both (I) and (III) above
(b) Only (II) above
(c) Both (II) and (IV) above
(d) Both (I) and (II) above
(e) All (I), (II), (III) and (IV) above.
230. Which of the following expressions will produce errors upon compilation?
I. boolean a = (boolean) 1;
II. boolean b = (false && true);
III. float y = 22.3;
IV. int x = (25 | 125)
(a) Both (I) and (III) above
(b) Only (I) above
(c) (I), (III) and (IV) above
(d) (I), (II) and (IV) above
(e) All (I), (II), (III) and (IV) above.

Answers

Answer
221. : (a)
Reason: Remaining all are invalid declarations.
222. Answer : (a)
Reason: Remaining choices are false
223. Answer : (a)
Reason: The content of array variable table after executing the following code is
100
010
001
224. Answer : (a)
Reason: Line 1 and line 2 only
A subclass of an abstract class can also be abstract if it does not define all the abstract methods in the
parent class.
225. Answer : (a)
Reason: don't use any keyword at all (make it default)
226. Answer : (a)
Reason: To make a member of a class visible in all subclasses regardless of what package they are in either private
OR protected
227. Answer : (a)
Reason: -263 to 263 – 1 The type long is a 64-bit two's complement that can be used for long integers.
228. Answer : (a)
Reason: Hexadecimal numbers start with "0x" in the Java programming language.
229. Answer : (a)
Reason: 238.88 is a double in Java. To assign this value to a float, 238.88f must be used. A variable of type boolean
cannot be converted to an integer in Java.
230. Answer : (a)
Reason: Integers cannot be converted to booleans, and floats must be explicitly specified (22.3f, and not 22.3 which
is a double).
231. Which of the following will produce an error?
I. byte a1 = 2, a2 = 4, a3;
II. short s = 16;
III. a2 = s;
IV. a3 = a1 * a2;
(a) Both (III) and (IV) above
(b) Only (II) above
(c) Only (III) above
(d) Only (IV) above
(e) Both (I) and (IV) above.
232. Examine the following code snippets to identify the legal loop constructs:
I. for (int i = 22, int j = 0; i + j > 11; i = i-3, j++)
{
... (Other statements)
}
II. int i = 100;
while(i)
{
... (other statements)
}
III. while (int i > 0)
{
i--;
... (other statements)
}
IV. int i = -10, sum = 0;
do {
... (other statements)
}
while(sum < 5 || i < 0);
(a) Both (I) and (IV) above
(b) Only (I) above
(c) Only (II) above
(d) Both (I) and (III) above
(e) All (I), (II), (III) and (IV) above.
233. When we invoke repaint () for a java.awt.Component object, the AWT invokes the method
(a) Insert()
(b) update()
(c) draw()
(d) show()
(e) paint().
234. What does the following line of code do?
Textfield text = new Textfield (10);
(a) Creates text object that can hold 10 columns of text
(b) Creates text object that can hold 10 rows of text
(c) Creates the object text and initializes it with the value 10
(d) The code is illegal
(e) Creates text object that can hold 10 columns of text and 10 rows of text.
235. Which of the following applet tags is legal to embed an applet class named Test into a Web page?
(a) < applet
code = Test.class width = 200 height = 100>
(b) < applet
class = Test width = 200 height = 100>
(c) < applet>
code = Test.class width = 200 height = 100>
(d) < applet
code = Test.class width = 200 height = 100
(e) < applet
param = Test.class width = 200 height = 100>.
236. Which of the following classes are available in the java.lang package?
I. Stack.
II. Object.
III. Math.
IV. String.
V. StringBuffer.
Choose the correct option from the following:
(a) (II), (III), (IV) and (V) above
(b) (II), (III) and (IV) above
(c) (II), (IV) and (V) above

(d) (III), (IV) and (V) above


(e) All (I), (II), (III), (IV) and (V) above.
237. Which of the following are the wrapper classes?
I. Random.
II. Byte.
III. Integer.
IV. Short.
V. Double.
(a) (II), (III), (IV) and (V) above
(b) (II), (III) and (IV) above
(c) (II), (III) and (V) above
(d) (III), (IV) and (V) above
(e) All (I), (II), (III), (IV) and (V) above.
238. Which of the following contain error?
I. int x[] = int[10];.
II. int[] y = new int[5];
III. x = y = new int[10];
IV. int a[] = {1, 2}; int b[]; b = a;
V. int i = new int(10);
(a) (I), (III) and (V) above
(b) (I), (III) and (IV) above
(c) (I), (IV) and (V) above
(d) (III), (IV) and (V) above
(e) All (I), (II), (III), (IV) and (V) above.
239. Which is the latest version of the Java language?
(a) Java 5.0
(b) Java 6.0 Alpha
(c) Java 6.0 Beta
(d) Java 7.0
(e) Java 7.0 Beta.
240. How to declare a class which has fields that are not serializable?
(a) Public
(b) Private
(c) Protected
(d) Variant
(e) Transient.
Answers

231. Answer : (a)


Reason: In line 3, a short value is being assigned to a variable of type byte, which is illegal. Also, the "*" operator
promotes the bytes to integers, which makes it illegal to assign the return value to a variable of type byte.
232. Answer : (a)
Reason: In option (B), the argument of "while" is an integer, which is illegal. It can only be Boolean. In option (C),
"int i > 0" is an illegal construct.
233. Answer : (a)
Reason: In option (B), the argument of "while" is an integer, which is illegal. It can only be Boolean. In option (C),
"int i > 0" is an illegal construct.
234. Answer : (a)
Reason: Creates text object that can hold 10 columns of text.
235. Answer : (c)
Reason: < applet
Code = Test. class width = 200 height = 100>
236. Answer : (a)
Reason: Object, Math, String, String Buffer belong to Java.lang package.1
237. Answer : (a)
Reason: Byte, integer, short and double are the wrapper classes.
238. Answer : (a)
Reason: Statements (I), (III) and (V) contain error.
239. Answer : (a)
Reason: Java 5.0 includes a number of new language features, most notably generic types, which increase both
the complexity and the power of the language.
240. Answer : (e)
Reason: We can declare them as transient.
241. Which of the following is not a valid String Constant?
(a) “X”
(b) ‘x’
(c) “Hello”
(d) “2002”
(e) “Hello Guys”.
242. Consider the class definition:
public class MyClass {
int x; int y;
void myprint(){
System.out.println(“x = ” +x);
System.out.println(“Y = ” +y);
}
}
Which of the following is a correct way to create an object?
(a) MyClass mc=new MyClass();
(b) MyClass mc;
(c) MyClass mc=new MyClass(10,20);
(d) MyClass mc=MyClass(10,20);
(e) All the above.
243. Which of the following is not true?
(a) Every time the class is instantiated, a new copy of each of the instance variables is created
(b) Every time the class is instantiated, a new copy of each of the class variables is created
(c) A class has only one copy of each of its static members, which are shared by all the instances
of the class
(d) Static members are accessed without an instance of a class
(e) An object is the instance of the class.
244. Which of the following is not necessarily related to RMI?
(a) Stub and skeleton
(b) Remote interface
(c) Graphical User Interface
(d) Client and Server
(e) Protocol.
245. Which of the following is not true for a constructor?
(a) A constructor is used to initialize an object
(b) A default constructor takes no parameter
(c) A constructor can be overloaded
(d) A constructor can be declared abstract
(e) A constructor access mutators are get() and set().
246. Which of the following is true for method overriding?
(a) A method in the subclass that has the same name, same arguments and same return type as a
method in the super class is orverridden
(b) An overridden method advocates for inheritance in OOP
(c) It is not possible to access the method which is overridden in a subclass
(d) Overridden method in java is called virtual function
(e) Overloading method in java is called virtual function.
247. Consider the following code:
interface A { int a=5; int b=10;}
Which of the following is possible for the above?
(a) class B implements A{int x;
void add(){ x= a+b; }
}
(b) class B extends A { int x;
void add(){ x= a+b; }
}
(c) class B implements A { int x;
void add(){ a=20; b=10; x= a+b; }
}
(d) class B extends A{ int x;
void add(){ a=20; b=10; x= a+b; }
}
(e) class A implements B{int x;
void add()
{ x=a+b;
}.
248. Which of the following package is automatically imported to your program file?
(a) java.util
(b) java.io
(c) java.net
(d) java.lang
(e) java.math.
249. Which of the following is not true for a java thread?
(a) The run method calls start method
(b) The start method calls run method
(c) Runnable interface contains only run method
(d) The run method is overridden to contain the functionality of the application
(e) Once we use the stop() method on a thread, we cannot use the start() method.
250. Which is not a valid operator in Java?
(a) <>
(b) !=
(c) =>
(d) <=
(e) +=.

Answers

241. Answer : (b)


Reason : ‘x’ is not a valid string constant.
242. Answer : (a)
Reason : correct way to create an object MyClass mc=new MyClass();
243. Answer : (b)
Reason : Every time the class is instantiated, a new copy of each of the class variables is created
244. Answer : (c)
Reason : GUI not necessarily related to RMI
245. Answer : (d)
Reason : A constructor can be declared abstract
246. Answer : (a)
Reason : A method in the subclass that has the same name, same arguments and same return type as a
method in the super class is orverridden
247. Answer : (a)
Reason : A is the right choice of declaration.
248. Answer : (d)
Reason : the java.lang package is automatically imported to your program file
249. Answer : (a)
Reason : The run method doesnot call start method.
250. Answer : (a)
Reason : <> is not a valid operator in java.
251. Which of the following is not a built-in exception found in java API?
(a) IOException
(b) NullPointerException
(c) ArithmaticException
(d) FileNotFoundException
(e) SystemFoundException.
252. Which of the following exception is caused when a math error such as division by zero occurs?
(a) ArithmaticException
(b) DivisionByZeroException
(c) NumberFormatException
(d) IOException
(e) ArrayIndex OutofBoundException.
253. Which of the following is not an Applet method?
(a) start()
(b) init()
(c) run()
(d) stop()
(e) destroy().
254. Which of the following is not a valid Java identifier?
(a) my Value
(b) $_AAA1
(c) width
(d) m_x
(e) my_value.
255. Which of the following method is inherited by Applet from its parent class?
(a) paint
(b) start
(c) init
(d) stop
(e) run.
256. Which of the following is a character stream?
(a) FileInputStream
(b) DataInputStream
(c) BufferedInputStream
(d) FileReader
(e) FileOutputStream.
257. Which of the following stream is/are used to handle primitive data types?
(a) BufferedOutputStream
(b) DataInputStream
(c) FileOutputStream
(d) FileInputStream
(e) All the above.
258. Which of the following stream is used for Object Serialization?
(a) DataOutputStream
(b) DataInputStream
(c) ObjectOutputStream
(d) ObjectWriter
(e) ObjectReader.
259. Every Java applet or application is composed of at least one
(a) Method
(b) Data member
(c) Class definition
(d) Imported class
(e) Package definition.
260. The extends keyword creates a new
(a) Instance
(b) Subclass
(c) Baseclass
(d) Superclass
(e) Object.

Answers

251. Answer : (e)


Reason : SystemFoundException is not a built-in exception.
252. Answer : (a)
Reason : Arithmetic Exception is caused when a math error such as division by zero occurs
253. Answer : (c)
Reason : run() is not an applet method
254. Answer : (a)
Reason : my Value (Identifiers may not contain blanks)
255. Answer : (a)
Reason : paint following method is inherited by Applet from its parent class.
256. Answer : (d)
Reason : FileReader is a character stream.
257. Answer : (b)
Reason : DataInputStream is used to handle primitive data types
258. Answer : (c)
Reason : ObjectOutputStream is used for Object Serialization
259. Answer : (c)
Reason : Class definition
260. Answer : (b)
Reason : subclass (the class is derived from the superclass)
261. When an applet container encounters an HTML file that specifies an applet to execute, the applet
container automatically loads ________ of the applet from the same directory as that of the HTML file.
(a) the .java file
(b) the .class file
(c) the main line
(d) the .htm file
(e) the .jva file.
262. Which of the following is the default Layout Manager for an Applet?
(a) FlowLayout
(b) BorderLayout
(c) GridLayout
(d) CardLayout
(e) MarginLayout.
263. Single-line comments that should be ignored by the compiler are denoted using
(a) Two forward slashes ( // )
(b) A slash and a star ( /* )
(c) System.out.println
(d) System.out.print
(e) One forward slash.
264. Which of the following escape sequences represents carriage return?
(a) \n
(b) \r
(c) \\
(d) \c
(e) \\r.
265. What is the value of result after the following Java statements execute?
int a, b, c, d;
a = 4;
b = 12;
c = 37;
d = 51;
result = d % a * c + a % b + a;
(a) 119
(b) 51
(c) 127
(d) 59
(e) 159.
266. List the following operators in the order that they will be evaluated: -, *, (), %, +, /. Assume that if two
operations have the same precedence, the one listed first will be evaluated first.
(a) (), /, *, %, +, -
(b) -, +, %, *, /, ()
(c) -, *, (), %, +, /
(d) (), *, /, %, -, +
(e) /, *, (), %, -, +.
267. If ClassA extends ClassB, then
(a) ClassA is a superclass
(b) ClassB is a base class
(c) ClassB is a subclass
(d) ClassB is a derived class
(e) ClassA is a base class.
268. Which exception is thrown by the getConnection() method of the DriverManager class?
(a) ConnectionNotFoundException
(b) IOException
(c) SQLException
(d) ReadException
(e) DataExecption.
269. Which of the following is valid to open a database connection?
(a) Connection con = DriverManager.getConnection(url, userid, password);
(b) Connection con = DriverManager.getConnection();
(c) Connection con = new DriverManager.getConnection();
(d) Connection con = DriverManager.openConnection(url);
(e) Connection con = new DriverManager.openConnection();.
270. What does the following code do?
smt=con.createStatement();
(a) A Prepared Statement object is created to send sql commands to the database
(b) A Statement object is created to send sql commands to the database
(c) A callable Statement is created to send sql commands to the database
(d) A Statement object is created to execute parameterise SQL commands
(e) A Default Statement object is created to send sql commands to the database.

Answers

261. Answer : (b)


Reason : the .class file
262. Answer : (a)
Reason : FlowLayout is the default Layout Manager for an Applet
263. Answer : (a)
Reason : Two forward slashes ( // ).
264. Answer : (b)
Reason : The escape sequence represents carriage return is . \r.
265. Answer : (a)
Reason : the result of the above expression is 119.
266. Answer : (d)
Reason : The correct order is (), *, /, %, -, +.
267. Answer : (b)
Reason : ClassB is a base class.
268. Answer : (c)
Reason : SQLException is thrown by the getConnection() method of the Driver Manager classs.
269. Answer : (a)
Reason : Connection con=DriverManager.getConnection(url, userid, password); is valid to open a database
connection
270. Answer : (b)
Reason : A Statement object is created to send sql commands to the database.
271. Select from among the following what Java can do?
(a) Object oriented applications can be developed
(b) Networking applications can be developed
(c) Database applications can be developed
(d) Either (a) or (b) above
(e) Either (a) or (b) or (c) above.
272. In addition to JDK there are a number of Java commercial development tools available for
Java programmers. Select from among the following such Java commercial development tool
available.
(a) Borland JBuilder
(b) Visual Basic.net
(c) Java Server Pages
(d) My sql server
(e) Java Script.
273. Which is identified as the first ever Object Oriented Programming Language?
(a) C++
(b) C
(c) Java
(d) Simula 67
(e) Small talk.
274. How comments that can be used in Automatic Class Documentation by javadoc tool are
represented?
(a) Started with **/ and Ended with */
(b) Started with /* and ended with */
(c) Started with /** and Ended with */
(d) Started with – and ended with –
(e) Started with /** and ended with /*.
275. Which one of the following is legal valid variable name among the following?
Suppose we create the following classes Account and OverdraftAccount to represent bank
accounts. An Overdraft Account is a subtype of a general Account.
/** Account represents a mutable bank account. **/
class Account {
double balance;
/** @effects constructs a new Account with
* balance = initialBalance */
Account(double initialBalance) {
this.balance = initialBalance;
}
/** Returns a textual description of the type of this account.
* @return the String "Account" */
String getAccountType() {
return "Account";
}
/** @effects prints to standard output the type of this account
* followed by the balance. */
void printBalance() {
System.out.println(this.getAccountType() + ": $" + balance);
}
}
/** OverdraftAccount represents a mutable bank account with a credit limit.*/
class OverdraftAccount extends Account {
double creditLimit;
/** @effects constructs a new OverdraftAccount with
* balance = initialBalance,
* creditLimit = initialCreditLimit */
OverdraftAccount(double initialBalance, double initialCreditLimit) {
super(initialBalance);
this.creditLimit = initialCreditLimit;
}
/** Returns a textual description of the type of this account.
* @return the String "OverdraftAccount" */
String getAccountType() {
return "OverdraftAccount";
}
}
(a) 1 More
(b) # Two
(c) @ Two
(d) Temp_val
(e) 1234 abc.
276. The method getAccountType() is invoked in the code for printBalance().
What is the compile-time type of the receiver of this method invocation?
(a) Object
(b) Account
(c) OverdraftAccount
(d) The answer varies depending on how printBalance() is invoked
(e) AccountType.
277. Suppose we now wish to print the balance information for an account with the following code:
Account myAccount = new OverdraftAccount(5, 10);
myAccount.printBalance();
Which of the following statement is true?
(a) The printed output is the string “Account”
(b) The printed output is the string “OverdraftAccount”
(c) The printed output is the string “Account: $5”
(d) The printed output is the string “OverdraftAccount: $5”
(e) There will be a compilation error because OverdraftAccount, the run-time type of
myAccount, does not define the printBalance() method.
278. Given the code below:
class A {
public void print() { System.out.println("hi"); }
}
class B extends A {
public void print() { System.out.println("bye"); }
}
What does each of the following code samples do:
A a = new B();
a.print();
(a) Prints nothing
(b) Prints “hi”
(c) Prints “bye”
(d) The code does not compile, the compiler returns a type error
(e) The code throws a runtime exception.
279. Given the code below:
class A {
public void print() { System.out.println("hi"); }
}
class B extends A {
public void print() { System.out.println("bye"); }
}
B b = new A();
b.print();
(a) Prints nothing
(b) Prints “hi”
(c) Prints “bye”
(d) The code does not compile, the compiler returns a type error
(e) The code throws a runtime exception.
280. What is the size of the Short integer type in terms of bits?
(a) 8
(b) 16
(c) 32
(d) 64
(e) 128.

Answers

271. Answer : (e)


Reason: With Java Object oriented applications , Networking applications, database applications, can be
developed.
272. Answer : (a)
Reason: Borland Jbuilder is the right choice as the other are for different purposes.
273. Answer : (d)
Reason: Simula67 is the first ever object oriented programming language.
274. Answer : (c)
Reason: Remaining are for single line and multiline comments
275. Answer : (d)
Reason: variable names should only Start with a Letter, Underscore (_) , or Dollar Sign ($).After the first letter
Name can include any letter or Number but cannot include Symbols such as %, @, * and so on.
276. Answer : (b)
Reason: T he receiver of the method invocation is implicitly "this", which, in the context of the Account class in
which the printBalance() resides, has a compile-type of Account. Note that an object has only one
compile-time type; thus, circling both Object and Account is incorrect, even though Account is a subtype
of Object.
277. Answer : (d)
Reason: OverdraftAccont.getAccountType() is called from printBalance() as myAccount has a runtime type of
OverdraftAccount.
278. Answer : (c)
Reason: T he print() method of the run-time type, B, is called.
279. Answer : (d)
Reason: The compiler will not allow you to set an object reference to an instance whose runtime type is a Java
supertype of the variable's compile time type.
280. Answer : (b)
Reason: Short integer in 16 bits or 2 bytes

281. What is the output of the following code?


class Arithmetic {
public static void main (string args[]) {
int x=17, y=5;
System.out.println(“x =” +x);
System.out.println(“y =” +y);
System.out.println(“x+y=” +(x+y));
System.out.println(“x-y=” +(x-y));
System.out.println(“x*y=” +(x*y));
System.out.println(“x/y=” +(x/y));
System.out.println(“x%y=” +(x%y));
}
}
(a) 5, 17, 12, 85, 3, 2
(b) 17, 5, 22, 12, 85, 3, 2
(c) 17, 5, 20, 21, 85, 3, 2
(d) 2, 3, 85, 12, 22, 5, 17
(e) 17, 5, 12, 22, 85, 2, 3.
282. What is the output of the following code?
class SpecialArithmetic {
public static void main (string args[]) {
int x=17,a,b;
a=x++;b=++x;
System.out.println(“x=” + x +“a=” +a);
System.out.println(“x=” + x + “b=” +b);
a=x--;b=--x;
System.out.println(“x=” + x + “a=” +a);
System.out.println(“x=” + x + “b=” +b);
}
}
(a) x=18 a=17
x=19 b=19
x=18 a=19
x=17 b=17
(b) x=19 a=17
x=18 b=19
x=18 a=19
x=17 b=17
(c) x=19 a=17
x=19 b=19
x=18 a=20
x=17 b=17
(d) x=19 a=17
x=19 b=19
x=18 a=19
x=17 b=17
(e) x=19 a=18
x=19 b=19
x=18 a=19
x=17 b=17.
283. What is the output of the following code?
class Bitwise {
public static void main (string args[]) {
int x=5, y=6;
System.out.println(“x&y=” +(x &y));
System.out.println(“x|y=” +(x|y));
System.out.println(“x^y=” +(x^y));
}
}
(a) x&y=5
x|y=7
x^y=3
(b) x&y=4
x|y=7
x^y=3
(c) x&y=6
x|y=7
x^y=3
(d) x&y=4
x|y=8
x^y=3
(e) x&y=4
x|y=8
x^y=4.
284. What is the output of the following code?
class Shift {
public static void main (string args[ ]) {
int x=7;
System.out.println(“x>>>1=” +(x>>>1));
}
}
(a) x>>>1=3
(b) x>>>1=6
(c) x>>>1=4
(d) x>>>1=5
(e) x>>>1=2.
285. class Relational{
public static void main (string args[]) {
int x=7, y=11, z=11;
System.out.println(“y<=z =” +(y<=z));
}
}
(a) y<=z = 0
(b) y<=z = 1
(c) y<=z = true
(d) y<=z = false
(e) y<z = true.
286. char chars[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’};
String s = new String(chars, 2, 3);
System.out.println (s);
What is the output of the above code?
(a) abc
(b) bcd
(c) cde
(d) deb
(e) acd.
287. String Firstname[]= new String[5]
String firstName [ ] = { “Kamal”, “Amal” , “Nimal
“Saman”, “Sunil” } ;
What is value of firstName[5]?
(a) Kamal
(b) Amal
(c) Saman
(d) Sunil
(e) Throws an exception.
288. int twoDMatrix [ ][ ] = new int [4][6];
int twoDMatrix [ ][ ] = { {5,1,2,8,6,9}
{8,4,1,6,2,1}
{3,9,2,5,8,9}
{1,7,3,6,4,3} };
What is the output of the following statement twoDMatrix[0].length?
(a) 3
(b) 4
(c) 5
(d) 6
(e) 7.
289. Which operator is used for concatenation in java?
(a) -
(b) Dot
(c) *
(d) %
(e) +.
290. class Point {
int x,y;
Point (int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return “Point[“ +x+ “,” +y+ “]”;
}}
class toStringDemo {
public static void main (String args[]) {
Point p = new Point (10,20);
System.out.println(“p=“ +p);}
}
What is the output of the following code?
(a) p = Point[10,20]
(b) p = Point[20,20]
(c) p = Point[20,10]
(d) p = Point[11,20]
(e) p = Point[10,21].

Answers

281. Answer : (b)


Reason: B is the right choice
282. Answer : (d)
Reason: According to the rule of increment operator the choice is D.
283. Answer : (b)
Reason: According to the rule of bitwise operator the choice is B
284. Answer : (a)
Reason: According to the rule of the Right Shift operator the following expression is evaluated to 3
285. Answer : (c)
Reason: According to the rule of the Relational operator precedence c is the apt choice.
286. Answer : (c)
Reason: String(chars,2,3) means starting at the position 2 extract 3 characters.here indexing start from o so first
character is c and remaining are d and e.
287. Answer : (e)
Reason: As the array is out of bound
288. Answer : (d)
Reason: The given matrix is of 4 rows and each row containing 6 elements.
289. Answer : (e)
Reason: + operator is used for concatenation in java
290. Answer : (a)
Reason: Shows a class which overrides toString to show the values of its instance variables.
291. String s1 = “Hello”;
String s2 = “Hello”;
System.out.println(s1 + “equals” + s2 + “->” + s1.equals(s2));
What is the output of the following code?
(a) Hello equals Hello -> 0
(b) Hello equals Hello -> 1
(c) Hello equals Hello -> false
(d) Hello equals Hello -> true
(e) Hello equals Hello -> hello.
292. Given below is the syntax of the method declaration in java.
Syntax: modifier returnType MethodName (parameter List) {
statement(s);
}
Which defines the Signature of the method?
(a) modifier
(b) modifier, returntype
(c) return type, method name
(d) returntype, methodname and parameter list
(e) modifier, returntype, methodname and parameter list.
293. What modifier is to be used If you wish to declare that you want to no longer allow subclasses
to override your variables or methods?
(a) Finally
(b) Abstract
(c) Final
(d) Synchronized
(e) Volatile.
294. Which access specifier is used for describing Applet Class?
(a) Private
(b) Public
(c) Protected
(d) Either (a) or (b)
(e) Either (b) or (c).
295. The following program takes any number of numeric arguments & returns the sum & the
average of those arguments. When this program run, is it throws an error? Find which line
number is to be modified to give the correct output?
Line 1)class SumAverage {
Line 2)public static void main(String args[]) {
Line 3)int sum = 0;
Line 4)for (int I = 0; i<args.length; i++) {
Line 5)Sum += args[i];
}
Line 6)System.out.println(“Sum is;” + sum);
Line 7)System.out.println(“Average is:” +
Line 8)(float) sum / args.length);
}
}
(a) Line 3
(b) Line 4
(c) Line 5
(d) Line 8
(e) Line 2.
296. Select the exception class from among the following which is part of the java.lang package.
(a) Inputstream exception
(b) IOException
(c) MalFormedURLException
(d) RunTimeException
(e) EOFException.
297. Consider the following variable declarations.
int a, b; String str;
Assume that at some point of the program, the variables a and b contain the values 5 and 7.
Select from among the following the correct option which assign the variable str with the value
“The sum of 5 + 7 = 12”?
(a) str = “The sum of a + b = a + b”;
(b) str = “The sum of “ + a +” + “+ b +” =” + a + b;
(c) str = “The sum of “+ a +” + “+ b +” =” + (a + b);
(d) str = “The sum of a + b =” + (a + b);
(e) str = “The sum of a + b = (a + b)”;.
298. Select from among the following character escape code which is notavailable in Java.
(a) \t
(b) \r
(c) \a
(d) \\
(e) \v.
299. Consider the following program written in Java.
class Selection{
public static void main(String args[]){ int x=7; if(x==2); //? Note the semicolon
System.out.println("Number seven");
System.out.println("Not seven"); } }
What would the output of the program be?
(a) Number seven
Not seven
(b) Error
(c) Not seven
(d) Number seven
(e) 7.
300. AWT stands for
(a) Advanced windowing tool kit
(b) Abstract Windowing Term Kit
(c) Abstract Windowing Tool Kit
(d) Applet Window Tool Kit
(e) Asynchronous windowing tool kit.

Answers

291. Answer : (d)


Reason: If we use the equals method in String It will return true if the single parameter is made up of the same
characters as the object you call equals on.
292. Answer : (d)
Reason: D is the right choice for method declaration.
293. Answer : (c)
Reason: Final modifier is to be used If you wish to declare that you want to no longer allow subclasses to
override your variables or methods?.
294. Answer : (b)
Reason: All applets must be declared public because the Applet class is a public class.
295. Answer : (c)
Reason: SumAverage.java.6: Incompatible type for +=. Can’t convert
java.lang.String to int.
Sum += args[i];
You have to convert them from strings to integers using a class method for the Integer class called
parseInt.
Sum += Integer.parseInt(args[i]);
296. Answer : (d)
Reason: Remaining all are contradictory to the given statement.
297. Answer : (c)
Reason: C is the right choice remaining all are not syntactically correct
298. Answer : (c)
Reason: \a is not the escape sequence character when compared to other.
299. Answer : (a)
Reason: A is the right choice .
300. Answer : (c)
Reason: AWT stands abstract Windowing Tool Kit.

You might also like