1.
public
class Sample {
public
static void main (String [] args) {
System.out.println ((1==1)? (7==8)? 5:6:4);
}
}
ANSWER:
6
EXPLANATION: (1==1)
returns true. So (7==8) gets evaluated which in turn returns false. So 6 will
get displayed.
2.
public
class Sample{
public
static void main (String [] args) {
System.out.println
(Math.round (Math.random ()));
}
}
ANSWER: prints
either 0 or 1 only
EXPLANATION:
Math.random returns a double value that is greater than or equal to 0.0 but
less than 1.0
3.
public
class Sample{
public
static void task (int no) {
System.out.print
(no%10);
if
((no/10) != 0)
task
(no/10);
System.out.print
(no%10);
}
public
static void main (String [] args) {
Sample.task
(1248);
}
}
ANSWER: 84211248
EXPLANATION:
Recursion is implemented using stacks. After recursive calls get finished, the
second print statement will be executed. Since stack models LIFO, what is
printed first by the recursive call will get processed last.
4.
public
class Sample{
public
static void main (String [] args) {
int
a=1, b=13;
if ((b< (a=a+a))
|| (b< (a=a+a)) || (b< (a=a+a)) || (b< (a=a+a)) || (b< (a=a+a)))
System.out.println (b-a);
else
System.out.println
(b+a);
}
}
ANSWER: -3
EXPLANATION: The ||
operator does not evaluate its right operand, if its left operand evaluated to
be true. When the 4th condition gets evaluated, the value of “a” becomes
8. So the condition returns true (13<8+8). Therefore (b-a) is nothing but
13-16=-3.
HINT: Try the conditional statement after
replacing || with && or &.
5.
Math.ceil(x) has the same value as –Math.floor(-x).
EXAMPLE: Math.ceil(5.3) is 6.0
-Math.floor(-5.3) is
also 6.0
HINT: Also Math.max(m,n)
is same as –Math.min(-m,-n) and
Math.floor(m+0.5) is equivalent to
Math.round(m)
6.
Object is the only class without a superclass. In
other words Object is the superclass of all the classes existing in java. If a class does not explicitly extend any
other class, it extends the Object class
by default.
7.
; is a statement that does nothing.
8.
Automatic
conversion
from primitive type to an Object of the corresponding Wrapper class
is called boxing.
EXAMPLE: int to Integer
boolean to Boolean
9.
public
class Sample{
public
static void main (String [] args) {
boolean
a=false, b=false;
if
(! a&&a)
System.out.println ("if");
else
System.out.println
("else");
}
}
ANSWER: else
EXPLANATION: The
condition (! a&&a) will be
interpreted as ((!a) && a). This evaluates to false.
HINT: If the condition is rewritten as (!
(a&&a)), it will be evaluated to true.
10.
Consider the following 2 statements & predict which
one will execute faster.
a.
a>b ? a : b
b.
Math.max(a,b)
EXPLANATION: a runs faster than b because of the function call overhead associated with b.
11.
public class Sample{
public static void main(String[]
args){
int s=1;
if(s)
System.out.println(s);
}
}
ANSWER: results in compilation error.
EXPLANATION: Java
cannot convert integer into boolean. In C/C++ 0 is false and any non-zero value
is true.
12.
public class Sample{
public static void main(String[]
args){
boolean [] ages = new
boolean[4];
System.out.println(ages[0]==ages[1]);
}
}
ANSWER: displays true.
EXPLANATION: The
elements of boolean array will be initialized to false by default. So false ==
false becomes true.
13.
public class Sample{
public static void main(String[]
args){
System.out.println((int)Math.ceil(Math.random()*40+10));
System.out.println((int)Math.round(Math.random()*40+10));
}
}
ANSWER: Returns a random integer in the range of 10 to 50(both inclusive).
14.
What is the impact of declaring a class, method and
variable as final?
ANSWER: Classes declared as
final cannot be extended.
Methods declared as final cannot
be overridden.
Variables declared as final can
be assigned only once.
16.
Categorize java Variables:
ANSWER:
Class Variable
Instance Variable
Local Variable
Array Component Variable
Method Parameter Variable
Constructor Parameter Variable
Exception Handler Parameter Variable
17.
Mention
the order in which user defined classes will be searched:
ANSWER:
By default looks into the current working directory (.).
Entries in the CLASSPATH environment variable, which overrides the default.
Entries in the -cp (or -classpath) command-line
option, which overrides the
CLASSPATH environment variable.
The runtime command-line option -jar, which overrides all the above.
18.
Why String has been made immutable in Java?
ANSWER: String has been made immutable
in Java to enhance 'Security’. The filenames in Java are specified by
using Strings. With Strings being immutable, JVM can make sure that the
filename instance member of the corresponding File object would keep pointing
to same unchanged "filename" String object. The 'filename' instance
member being a 'final' in the File class can anyway not be modified to point to
any other String object specifying any other file than the intended one (the
one which was used to create the File object).
19.
Brief about Garbage Collection.
ANSWER: Objects
are created on heap in Java.
Garbage collection is a mechanism
provided by JVM to reclaim heap
space from Objects which are eligible for Garbage collection. Object
becomes eligible for garbage collection if all of its references are null.
Garbage
collection relieves
Java programmer from memory management, which is essential part of C++
programming and gives more time to focus on business logic.
Garbage Collection is carried by a daemon
thread called Garbage Collector.
Before removing an Object
from memory, Garbage collection thread
invokes finalize() method of that Object and gives an opportunity to
perform any sort of cleanup required.
A Java programmer cannot
force Garbage collection; it will only trigger if JVM thinks it
needs a garbage collection based on Java heap size.
There are methods like System.gc
() and Runtime.gc () which is
used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will
happen.
If there is no memory space for
creating new Object in Heap JVM throws OutOfMemoryError.
20.
Coercion is
another name for an implicit typecast, i.e. one mandated by the
language rules, and not explicitly added by the programmer.
21.
Differentiate
final, finally and finalize.
ANSWER:
final is a keyword/modifier. Refer answer no. 14.
finally is a block. The
finally block is executed even if an unexpected exception occurs. But finally
is useful for more than just exception handling - it allows the programmer to avoid
having cleanup code accidentally bypassed by a return, continue, or break
statement. Putting cleanup code in a finally block is always a good practice,
even when no exceptions are anticipated.
finalize is a method. Before an
object is garbage collected, finalize() method gets invoked. We can write
system resources release code in finalize() method before getting garbage collected.
finalize() neither takes any parameter nor returns anything.
22.
Let n be a variable of
datatype byte. The value of n<<m is same as n * 2m.
EXPLANATION: Once shifting left
is multiplying by 2
EXAMPLE: byte representation of 13 is 0000 1101
13<<4
becomes 1101 0000 (208) [13 * 24
= 208]
23.
Can we declare a
static variable inside a method?
Static variables are Class level
variables and they can't be declared inside a method. If declared, the
class will not compile.
24.
Why is an Interface
be able to extend more than one Interface but a Class can't extend more than
one Class?
Basically Java doesn't
allow multiple inheritance, so a Class
is restricted to extend only one Class. But an Interface is a pure
abstraction model and doesn't have inheritance hierarchy like classes(do
remember that the base class of all classes is Object). So an Interface is
allowed to extend more than one Interface.
25.
If a Class is declared without any access
modifiers, where may the Class be accessed?
A Class that is
declared without any access modifiers
is said to have package access. This
means that the Class can only be accessed by other Classes that are defined
within the same package.
26.
What is the
preferred size of a Component?
The preferred size of a
Component is the minimum Component
size that will allow the Component to display
normally.
27.
What are the differences between Applets and
Applications?
Application:
-Applications are Stand
Alone and doesn’t need web-browser.
-Execution starts with main().
-May or may not be a GUI.
Applet:
-Needs no explicit
installation on local machine. Can be transferred through Internet on to the
local machine and may run as part of web-browser.
-Execution
starts with init () method.
-Must
run within a GUI. (Using AWT/Swing)
28.
Can we write abstract methods in a
final class and vice versa?
Abstract methods
are not allowed in final class. An abstract class can have final methods in it.
29.
Why do we require public static void main (String
args[]) method in Java program?
public: The method can be accessed outside
the class/package.
static: You need not have an instance of the class
to access the method.
void: Your
application need not return a value, as the JVM launcher would return the value
when it exits.
main(): This is the entry point for the application.
30.
What would you use to compare
two String variables - the operator == or the method equals()?
I'd use the method equals() to compare the values of the Strings and the operator == to check if two variables point at the same instance of a String object.
31.
Why abstract and final
classes are mutually exclusive?
A class
cannot be both final and abstract for obvious reasons. The abstract modifier says it's
not complete and no instance can be created for the class; some subclass
should provide a concrete implementation of any such abstract method. The final modifier says that the class is complete and cannot be extended. This
puts the mutual exclusion of the two.
32.
Why we cannot override
static methods?
We can
declare static methods with same signature in subclass, but it is not
considered overriding as there won’t be any run-time polymorphism. Hence the
answer is ‘No’. For class
(or static) methods, the method according to the type of reference is called, not according to the object being
referred, which means method call is
decided at compile time. For instance
(or non-static) methods, the method is called according to the type of object being referred, not
according to the type of reference, which means method call is decided at run time.
EXAMPLE:
// Superclass
class Base {
//
Static method in base class which will be hidden in subclass
public
static void display() {
System.out.println("Static
or Class method from Base");
}
//
Non-static method which will be overridden in derived class
public
void print() {
System.out.println("Non-static
or Instance method from Base");
}
}
// Subclass
class Derived extends Base {
//
This method hides display() in Base
public
static void display() {
System.out.println("Static
or Class method from Derived");
}
// This method overrides print() in Base
public
void print() {
System.out.println("Non-static
or Instance method from Derived");
}
}
// Driver class
public class Sample
{
public
static void main(String args[ ]) {
Base
obj1 = new Derived();
// As per overriding rules this should call to
class Derived's static overridden method. Since static method cannot be
overridden, it calls Base's display()
obj1.display();
// Here overriding works and Derived's print()
is called
obj1.print();
}
}
OUTPUT:
Static or
Class method from Base
Non-static
or Instance method from Derived
33.
What must a Class do to
implement an Interface?
If a Class
wants to implement an interface, it should use implements keyword in its Class declaration and provide the definitions to the methods of that Interface or else it should be declared as an abstract class. An Interface specifies what a Class
must do. So it can also be understood as
an agreement with the implementing Class.
34.
Define Immutable Class. How
to make a Java Class immutable?
An Immutable Class is one whose state cannot be changed once created.
Some guidelines
to make a class immutable:-
a) Don’t provide “setter” methods
This principle
says that for all mutable properties in your class, do not provide setter
methods. Setter methods are meant to change the state of an object and this is
what we want to prevent here.
b) Make all fields final and private
This is
another way to increase immutability. Fields declared private will not be
accessible outside the class and making them final will ensure that even
accidentally you cannot change them.
c) Don’t allow subclasses to override methods
The
simplest way to do this is to declare the class as final. Final classes in java
cannot be overridden.
35.
What is the disadvantage of
using threads?
Threads also have some
disadvantages. For example threads
are not reusable as they are dependent
on a process and cannot be separated from the process. Threads are not isolated as they don't have their own address space. The error caused by the thread can
kill the entire process or program, because that error affects the entire
memory space of all threads used in that process or program.
36.
What is the difference
between exception and error in java?
Error: Any deviation from the expected behavior of the system or program,
which stops the working of the system
is an error. Errors are exceptional scenarios that are out of scope of
application and it’s not possible to anticipate and recover from them. For
example hardware failure, JVM crash or
out of memory error. Some of the common Errors are OutOfMemoryError and StackOverflowError.
Exception: Any error or problem which one
can handle and continue to work normally. Bad programming also causes exception. Some of the exceptions are FileNotFoundException and ArrayIndexOutOfBoundException.
37.
What is the difference between creating a thread by
extending Thread class and by implementing Runnable interface?
The most common difference is
When you extends Thread class, after that you can’t extend any other
class which you required. (As you know, Java does not allow inheriting more
than one class).
When you implements Runnable, you can save a space for your class to
extend any other class in future or now.
However, the significant difference is
When you extends Thread class,
each of your threads has a unique object
associated with it.
When you implements Runnable,
many threads can share the same Runnable
instance.
EXAMPLE:
class
ImplementsRunnable implements Runnable {
private
int counter = 0;
public
void run() {
counter++;
System.out.println("ImplementsRunnable
: Counter : " + counter);
}
}
class ExtendsThread extends Thread {
private
int counter = 0;
public
void run() {
counter++;
System.out.println("ExtendsThread
: Counter : " + counter);
}
}
public class Sample {
public
static void main(String args[]) throws Exception {
//Multiple
threads share the same object.
ImplementsRunnable
ir = new ImplementsRunnable();
Thread
t1 = new Thread(ir);
t1.start();
Thread.sleep(1000);
// Waiting for 1 second before starting next thread
Thread
t2 = new Thread(ir);
t2.start();
Thread.sleep(1000);
Thread
t3 = new Thread(ir);
t3.start();
//Creating
new instance for every thread access.
ExtendsThread
et1 = new ExtendsThread();
et1.start();
Thread.sleep(1000);
ExtendsThread
et2 = new ExtendsThread();
et2.start();
Thread.sleep(1000);
ExtendsThread
et3 = new ExtendsThread();
et3.start();
}
}
OUTPUT:
ImplementsRunnable : Counter : 1
ImplementsRunnable : Counter : 2
ImplementsRunnable : Counter : 3
ExtendsThread : Counter : 1
ExtendsThread : Counter : 1
ExtendsThread : Counter : 1
EXPLANATION:
In the Runnable interface approach, only one
instance of a class is being created and it has been shared by different
threads. So the value of counter is incremented for each and every thread
access. Whereas, in the Thread class approach, you must have to create separate
instance for every thread access. Hence different memory is allocated for every
class instances and each has separate counter, the value remains same, which
means no increment will happen because none of the object reference is same.
38.
What is an Object and why initialization is important?
An object is a chunk of memory
bundled with the code that manipulates memory. In the memory, the object
maintains its state (the values of its instance variables), which can change
and evolve throughout its lifetime.
Java makes certain that memory (object's instance
variables) is initialized, at least to predictable default values, before it is
used by any code. Initialization is
important because, historically, uninitialized data has been a common source of
bugs. Local variables are not given default initial values. They must be
initialized explicitly before they are used.
39.
How can one prove that the
array is not null but empty?
Print array.length. If it prints 0, it is empty, means array has been
initialized with no elements. But if it would have been null then it would
have thrown a NullPointerException on attempting to print array.length.
EXPLANATION:
public class
Sample{
public static void main(String
args[]){
int a[]={};
System.out.println(a.length);
}
}
OUTPUT:
0 (So array a is understood to be
empty)
40.
What is a layout manager?
A layout manager is
an object that is used to organize components in a container.
41.
*Which of the
following statement is correct?
a)
A
reference is stored on heap.
b)
A
reference is stored on stack.
c)
A
reference is stored in a queue.
d)
A
reference is stored in a binary tree.
Choose the most appropriate
answer.
42.
*Which of these will
create and start this thread?
public class
MyRunnable implements Runnable{
public void run(){
//some code here.
}
}
a)
new
Runnable(MyRunnable).start();
b)
new
Thread(MyRunnable).run();
c)
new
Thread(new MyRunnable()).start();
d)
new
MyRunnable().start();
Choose the most
appropriate answer.
43.
*In Java, String is
________
a)
Array
of characters.
b)
An
object of String class.
c)
A
sequence of characters.
d)
Both
a and c.
Choose the most appropriate answer.
44.
*public class
Sample{
public
static void main(String[] args){
System.out.println("Hello
"+args[0]);
}
}
What will be the output of the program, if this code is executed as
follows in the command line?
>java Sample world
a) Hello
b) Hello Sample
c) Hello world
d) the code does not run
Choose the most
appropriate answer.
45.
What is the seed value being
used in the no argument constructor of a Random object? Is it possible to
generate identical sequence of random values using Random class?
Random() - constructs a Random object with the current time from System clock as its seed. Different Random
objects that are created in close succession by a call to the default
constructor will have identical default seed values and, therefore, will
produce identical sets of random numbers. Also by constructing Random objects with a specified seed will generate identical
sequence of random values.
EXAMPLE:
import java.util.*;
public class Sample{
public static void main(String[] args){
Random r1 = new Random(8);
Random r2 = new Random(8);
System.out.print("from random1: ");
for(int i=0;i<5;i++)
System.out.print(r1.nextInt(100)+"
");
//returns a random int value bet 0 and 100
System.out.print("\n from random2: ");
for(int i=0;i<5;i++)
System.out.print(r2.nextInt(100)+"
");
}
}
OUTPUT:
From
random1: 64 56
30 21 72
From
random1: 64 56
30 21 72
APPLICATION: In
software testing we can test our program using a fixed sequence of numbers.
46.
How Java achieves platform
independency?
The JVM takes on the
responsibility of interacting with the hardware rather than forcing the Java
programmer to write code specific to each hardware platform. Source code is
converted into an intermediate format known as byte code by JAVAC (These are not machine instructions but just an
intermediate format - format between Java source code and machine instruction).
Java byte codes obtained from javac can be executed on any platform and OS,
provided JVM is installed in it. So, we can say that JVM is platform dependent but Java is platform independent.
47.
accessor (getter) method Vs
mutator(setter) method.
Getters or accessors, are methods that provide access to an object's instance variables.
Prefixing the corresponding instance variable with "get" is a common
naming convention for getters.
public String getAuthor(){
return author;
}
Setters or mutators, are methods
that provide the caller with an opportunity to update the value of a particular
instance variable. Similar to getters, setters are often named by prefixing
the corresponding instance variable with "set".
public void setAuthor(String author){
this.author = author;
}
Note that, unlike getters,
setters have no return value. A setter's job is usually limited to changing the
value of an instance variable.
48.
Predict the output of the
following code and justify your answer.
public class Sample{
void show(Object o){
System.out.println("Object");
}
void show(String s){
System.out.println("String");
}
public static void main(String[]
args){
new Sample().show(null);
}
}
OUTPUT: String
EXPLANATION: That is because String class
extends from Object and hence is more specific than Object. Compiler always
chooses the most specific method to invoke. The Java programming language uses the rule that the most specific
method is chosen.
HINT: If you try to add a method that
take an Integer input it will throw error
as ambiguous methods, because String and
Integer both of them are more specific than Object but none is more specific than the other one.
49.
Examine the code given below
and find the error.
class
Super{
public int getLength(){
return 4;
}
}
public
class Sample extends Super{
public long getLength(){
return 8;
}
public static void main(String[]
args){
Super s = new Super();
Sample ss = new
Sample();
System.out.println(s.getLength()+","+ss.getLength());
}
}
ERROR: getLength() in Sample cannot override
getLength() in Super; attempting to use incompatible return type.
50.
public class Sample{
public static void main(String
args[]){
System.out.println("no.
of args : "+args.length);
}
}
What will
be the output, when the above code is executed in command line as
a)
>java Sample “1 2 3” 4 OUTPUT: 2
b)
>java Sample “*” OUTPUT: 1
c) >java
Sample OUTPUT: 0
d) >java
Sample * OUTPUT: returns the number of directories & files in
pwd
51.
Does the following code
resize the array?
public class Sample{
public
static void main(String[] args){
int[]
mylist;
mylist
= new int[10];
//int[]
mylist = {9,8,7,6,5,4,3,2,1,0};
//System.out.println(mylist.length);
//for(int
i=0;i<mylist.length;i++)
// System.out.print(mylist[i]+" ");
// sometime later assign a new array to
mylist
mylist = new int[20];
//System.out.println("\n"+mylist.length);
//for(int
i=0;i<mylist.length;i++)
// System.out.print(mylist[i]+" ");
}
}
ANSWER: In
this example, initially the array has
size 10. Once you've decided that the array has 10 elements, you can't make it any bigger or smaller. If
you want to change the size, you need to create a new array of the desired
size, and then copy elements from the old array to the new array, and use the
new array. Here, we are making the reference of old array pointed to the newly
created array. If we try to display the elements of the new array, old array
elements won’t be there.
52.
*Consider the entities – Quadrilateral,
Parallelogram and Rectangle. To model these entities in an object-oriented way,
they should be modeled as individual classes such that ___________________________________________.
53.
*An interactive program asks the question----“Are you right-handed,
left-handed or ambidextrous? Type R if right-handed, L if left-handed, A if
ambidextrous.” The part of the program(written by student A) that does the appropriate
action depending on the user’s response is given below.
if
(response == ‘A’) //c1
doThisForAmbidextrous();
else if
(response == ‘R’) //c2
doThisForRightHanded();
else if
(response == ‘L’) // c3
doThisForLeftHanded();
Student
B did the same thing but checked the conditions in the order - c3, c2 and c1.
Student
C did the same thing but checked the conditions in the order – c2, c3 and c1.
Student
D did the same thing but checked the conditions in the order – c2, c1 and c3.
One of these 4 students got special
recognition from the Professor for the order in which the student checked the
conditions. Who is the student?
54.
How objects will be created
in Java?
By using new keyword. This is
the most common way to create an object in java. Almost 99% of objects are created in this way.
55.
What makes the difference
between creating String objects in the following two ways?
a) String s = “Welcome”;
b)
String s = new
String(“Welcome”);
ANSWER:
a)
Creates String
literal. String literals work with a concept of String pool. When you create
a second String literal with same content, instead of allocating a new
space, Compiler will return the same
reference. Hence you will get true when you compare those two literals
using == operator.
For
example String s1=”Hello”; String s2=”Hello”; s1==s2 returns true. APPLICATION:
when we come across repeated contents, this is the best option which saves
memory and improves performance.
b)
Creates String
object. Each time JVM will create a
new object for each new keyword. For example String s1 = new
String(“Success”); String s2 = new String(“Success”); s1==s2 returns false.
HINT: If you want to create a new string object using second case and also you
don't want a new object, then you can use intern() method to get the same
object.
String s1 = "hello";
String s2 = new
String("hello").intern();
System.out.println(s1
== s2);
In this case instead of creating a new object, JVM will return the same
reference s. So the output will be true.
56.
What will happen to the
Exception object after exception handling?
It will become eligible for garbage collection and will be destroyed
soon by Garbage Collector and the memory will be freed.
57.
*What will be the result of compiling the following
code?
public
class Sample{
public static void main (String args
[]){
int age;
age = age + 1;
System.out.println("The
age is " + age);
}
}
a. Compiles and runs with no output
b. Compiles and runs printing out The age
is 1
c. Compiles but generates a runtime error
d. Does not compile
Choose the most appropriate answer.
58.
*What does the zeroth element of the String array passed to
the public static void main method contain?
a. The name of the program
b. The number of arguments
c. The first argument if one is present
Choose the most appropriate answer.
59.
*Which of these is the correct format to be used to create the literal
char value a?
a. ‘a’
b. "a"
c. new Character(a)
d. \000a
Choose the most appropriate answer.
60.
*Which of the following is illegal?
a. int i = 32;
b. float f = 45.0;
c. double d = 45.0;
Choose the most appropriate answer.
61.
* Which of the following return true?
a. "Ram" == "Ram"
b. "Ram".equals("Ram")
c. "Ram".equals(new String("Ram"))
Select
all the correct answers.
62.
*Which of the following do not lead to a runtime error?
a. "Ashok" + " is " + "
here"
b. "We" + 3
c. 3 + 5
d. 5 + 5.5
Select
all the correct answers.
63.
* Which of the following are acceptable?
a. Object o = new Button("START");
b. Boolean flag = true;
c. Panel p = new Frame();
d. Frame f = new Panel();
e. Panel p = new Applet();
Select
all the correct answers
64.
*What is the result of compiling and running the
following code?
public class Sample{
static int total = 10; //LINE2
public static void main (String
args []){
new
Sample();
}
public Sample(){
System.out.println("In
Sample");
System.out.println(this);
int temp =
this.total; //LINE9
if
(temp > 5){
System.out.println(temp);
}
}
}
a. The
class will not compile
b. The
compiler reports an error at line 2
c. The
compiler reports an error at line 9
d. The
value 10 is printed to the standard output
e. The
class compiles but generates a runtime error
Choose
the most appropriate answer.
65.
*Which of the following is correct?
a. String temp [] = new String {"t"
"e" "a" };
b. String temp [] = { "e" "a"
"t"};
c. String temp = {"a", "t",
"e"};
d. String temp [] = {"a",
"b", "c"};
Choose the most appropriate answer.
66.
*Under what situations do you obtain a default
constructor?
a. When you define any
class
b. When the class has no
other constructors
c. When you define at least
one constructor
Choose the most
appropriate answer.
67.
*Which of the following are acceptable to the Java compiler?
a. if (2 == 3)
System.out.println("Hi");
b. if (2 = 3)
System.out.println("Hi");
c. if (true) System.out.println("Hi");
d. if (2 != 3)
System.out.println("Hi");
e. if (myStr.equals("hello"))
System.out.println("Hi");
Select
all the correct answers.
68.
* What is the result of executing the following code, using the parameters
4 and 0?
public
void divide(int a, int b){
try{
int c = a / b;
}
catch (Exception e){
System.out.print("Exception ");
}
finally{
System.out.println("Finally");
}
}
a. Prints out: Exception Finally
b. Prints out: Finally
c. Prints out: Exception
d. No
output
Choose
the most appropriate answer.
69.
*In the following code, which is the earliest
statement, where the object originally held in e, may be garbage collected?
1. public class Test{
2. public
static void main (String args []){
3. Employee
e = new Employee("Raj", 44);
4. e.calculatePay();
5. System.out.println(e.printDetails());
6. e
= null;
7. e
= new Employee("Ram", 26);
8. e.calculatePay();
9. System.out.println(e.printDetails());
10. }
11. }
a. Line 10
b. Line 11
c. Line 7
d. Line 8
e. Never
Choose the most
appropriate answer.
70.
Which of the following
layout managers honors the preferred size of a component?
a. CardLayout
b. FlowLayout
c. BorderLayout
d. GridLayout
Choose the most
appropriate answer.
71.
*Consider the following example:
class First{
public First (String
s){
System.out.println(s);
}
}
public class Second extends First{
public static void
main(String args []){
new
Second();
}
}
What is the result of compiling and running the Second class?
a. Nothing happens
b. A string is printed to
the standard out
c. An instance of the class
First is generated
d. An instance of the class
Second is created
e. An exception is
raised at runtime stating that there is no null parameter constructor in class
First
f. The class second
will not compile as there is no default constructor in the class First
Choose the most
appropriate answer.
72.
*Consider the following class:
public class Test{
public static void
test(){
this.print();
}
public static void
print(){
System.out.println("Test");
}
public static void
main(String args []){
test();
}
}
What is
the result of compiling and running this class?
a. The string Test is printed to the
standard output.
b. A runtime exception is raised stating
that an object has not been created.
c. Nothing is printed to the standard
output.
d. An exception is raised stating that the
method test cannot be found.
e. The class fails to compile stating that the variable this cannot be
referenced inside a static method.
Choose the
most appropriate answer.
73.
* Examine the following class definition:
public class Test{
public static void
test(){
print();
}
public static void
print(){
System.out.println("Test");
}
public void
print(){
System.out.println("Another
Test");
}
}
What is
the result of compiling this class?
a. A successful compilation.
b. A warning stating that the class has no
main method.
c. An error stating that there is a
duplicated method.
d. An error stating
that the method test() will call one or other of the print() methods.
Choose the most appropriate answer.
74.
*Given the following sequence of Java statements
1. StringBuffer sb = new
StringBuffer("abc");
2. String s = new String("abc");
3. sb.append("def");
4. s.append("def");
5. sb.insert(1, "zzz");
6. s.concat(sb);
7. s.trim();
Which of
the following statements are true?
a. The compiler would generate an error for line 1.
b. The compiler would
generate an error for line 2.
c. The compiler would
generate an error for line 3.
d. The compiler would
generate an error for line 4.
e. The compiler would
generate an error for line 5.
f. The compiler would
generate an error for line 6.
g. The compiler would
generate an error for line 7.
Select
all the correct answers.
75.
*Carefully examine the following class:
public
class Sample{
public static void main(String args
[]){
/*This is the start of a
comment
if (true){
Sample obj =
new Sample();
System.out.println("Done
the test");
}
/* This is another
comment */
System.out.println
("The end");
}
}
What is
the result:
a. Prints
out "Done the test" and nothing else.
b.
Program produces no output but terminates correctly.
c.
Program does not terminate.
d. The
program will not compile.
e. The
program generates a runtime exception.
f. The
program prints out "The end" and nothing else.
g. The
program prints out "Done the test" and "The end".
Choose the most appropriate answer.
76.
Can we add the same
Component to more than one Container?
Each GUI component can be contained only once. If a
component is already in a container and you try to add it to another container,
the component will be removed from the first container and then added to the
second.
77.
You can create a String
object as String s = "abc"; Why can’t a Button object be created as
Button b = "abc"?
The main
reason you cannot create a button by Button b= “abc”; is because “abc” is a String
literal and b is a Button object. The
only object in Java that can be assigned a literal String is java.lang.String.
Important to note that you are NOT
calling a java.lang.String constructor, when you type String s = “abc”;
78.
What is a native method?
A native
method is a Java method (either an instance method or a class method) whose implementation is written in another
programming language such as C.
79.
What are all can be
referenced by this variable?
Within an instance method or a constructor, this is a reference to the
current object — the object whose method or constructor is being
called. You can refer to any member of
the current object from within an instance method or a constructor by using
this.
80.
Can a .java file contain more than one java classes?
Yes, a
.java file can contain more than one java classes. But there are two
restrictions:
•A .java
source file can contain at most one top-level class defined by the public
modifier.
•The
.java file name must be the same as the public class name.
Tags : Amazon Interview Papers , Amazon Placement Papers , Amazon Interview Question , Amazon Interview JAVA Questions , Amazon Interview , Amazon Interview Papers , Amazon Placement Papers , Amazon Interview Question , Amazon Interview JAVA Questions , Amazon Interview , Google Interview Papers , Google Placement Papers , Google Interview Question , Google Interview JAVA Questions , Google Interview , Google Interview Papers , Google Placement Papers , Google Interview Question , Google Interview JAVA Questions , Google Interview , Microsoft Interview Papers , Microsoft Placement Papers , Microsoft Interview Question , Microsoft Interview JAVA Questions , Microsoft Interview , Microsoft Interview Papers , Microsoft Placement Papers , Microsoft Interview Question , Microsoft Interview JAVA Questions , Microsoft Interview , TCS Interview Papers , TCS Placement Papers , TCS Interview Question , TCS Interview JAVA Questions , TCS Interview , TCS Interview Papers , TCS Placement Papers , TCS Interview Question , TCS Interview JAVA Questions , TCS Interview , Wipro Interview Papers , Wipro Placement Papers , Wipro Interview Question , Wipro Interview JAVA Questions , Wipro Interview , Wipro Interview Papers , Wipro Placement Papers , Wipro Interview Question , Wipro Interview JAVA Questions , Wipro Interview , HCL Interview Papers , HCL Placement Papers , HCL Interview Question , HCL Interview JAVA Questions , HCL Interview , HCL Interview Papers , HCL Placement Papers , HCL Interview Question , HCL Interview JAVA Questions , HCL Interview , Zoho Interview Papers , Zoho Placement Papers , Zoho Interview Question , Zoho Interview JAVA Questions , Zoho Interview , Zoho Interview Papers , Zoho Placement Papers , Zoho Interview Question , Zoho Interview JAVA Questions , Zoho Interview , Accenture Interview Papers , Accenture Placement Papers , Accenture Interview Question , Accenture Interview JAVA Questions , Accenture Interview , Accenture Interview Papers , Accenture Placement Papers , Accenture Interview Question , Accenture Interview JAVA Questions , Accenture Interview , Infosys Interview Papers , Infosys Placement Papers , Infosys Interview Question , Infosys Interview JAVA Questions , Infosys Interview , Infosys Interview Papers , Infosys Placement Papers , Infosys Interview Question , Infosys Interview JAVA Questions , Infosys Interview , Facebook Interview Papers , Facebook Placement Papers , Facebook Interview Question , Facebook Interview JAVA Questions , Facebook Interview , Facebook Interview Papers , Facebook Placement Papers , Facebook Interview Question , Facebook Interview JAVA Questions , Facebook Interview , Interview Papers , Placement Papers , Interview Question , Interview JAVA Questions , Interview , Interview Papers , Placement Papers , Interview Question , Interview JAVA Questions , Interview