java mcqs1-1
java mcqs1-1
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*/}.
class argumentPassing
{
public static void main(String args[])
{
test ob = new test();
int a = 15, b = 20;
(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.
int result;
if (n==1)
return 1;
result = puzzel(n-1) * n;
43. return result;
}
}
class prob2{
}
}
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 {
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.
//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
Answers
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.
Answers
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
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
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
ANSWERS:
1 2 3 4 5 6 7 8 9 10
B B A C D A B C B A
Inheritance
Polymorphism
Member function
Encapsulation
_____________________________________________________________________________________
Runtime polymorphism is achieved by
Friend function
Virtual function
Operator overloading
Function overloading
_____________________________________________________________________________________
Access to private data
38
25
9
12
_____________________________________________________________________________________
In a class specifier ,data or function designated private are accessible
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
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
Run-time error
Compile time error
Logical error
All of the above
_____________________________________________________________________________________
Function templates can accept
Abstraction
Inheritance
Encapsulation
Polymorphism
_____________________________________________________________________________________
The operator << when overloaded in a class
fbase();
fder();
base::fbase();
der::fder();
_____________________________________________________________________________________
In which case is it mandatory to provide a destructor in a 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
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
An abstract class
A class with a virtual function
A base class
None of the above
_____________________________________________________________________________________
Member functions, when defined within the class specification
++
::
~
()
_____________________________________________________________________________________
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?
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?
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
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
(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
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 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 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");
}
}
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.
Answers
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
Answers
Answers
Answers
Answers
Answers
Answers