Comprehensive Viva of Java
Comprehensive Viva of Java
What is a Class?
What is an Object?
What is an Instance?
Ans:An instance has state, behaviour and identity. The structure and
behaviour of similar classes are defined in their common class. An
instance is also called as an object.
Ans:Platform independence means that we can write and compile the java
code in one platform (eg Windows) and can execute the class in any other
supported platform eg (Linux,Solaris,etc).
1
loops.
c. When an object is not required, its reference should be nullified.
d. We should minimize the usage of String object and SOP's.
2
Ans: public- main(..) is the first method called by java environment when
a program is executed so it has to accessible from java environment.
Hence the access specifier has to be public.
void: main does not return anything so the return type must be void
The argument String indicates the argument type which is given at the
command line and arg is an array for string given during command line.
Ans:When a class inherits from more than class, it will lead to the
diamond problem - say A is the super class of B and C & D is a subclass of
both B and C. D inherits properties of A from two different inheritance
paths ie via both B & C. This leads to ambiguity and related problems, so
multiple inheritance is not allowed in Java.
3
Ans:With respect to multithreading, synchronization is the capability to
control the access of multiple threads to shared resources. Without
synchonization, it is possible for one thread to modify a shared variable
while another thread is in the process of using or updating same shared
variable. This usually leads to significant errors.
Ans:Java does not allow multiple inheritance for classes (ie. a subclass
being the extension of more than one superclass). To tie elements of
different classes together Java uses an interface. Interfaces are similar to
abstract classes but all methods are abstract and all properties are static
final. As an example, we will build a Working interface for the subclasses
of Animal. Since this interface has the method called work(), that method
must be defined in any class using the Working interface.
When you create a class that uses an interface, you reference the
interface with the reserved word implements Interface_list. Interface_list
is one or more interfaces as multiple interfaces are allowed. Any class
that implements an interface must include code for all methods in the
interface. This ensures commonality between interfaced objects.
4
super(nm); // builds ala parent
}
public void work() // this method specific to WorkingDog
{
speak();
System.out.println("I can herd sheep and cows");
}
}
What is a destructor?
5
Ans:Destructor is an operation that frees the state of an object and/or
destroys the object itself. In Java, there is no concept of destructors. Its
taken care by the JVM.
In the above command line Hello is the class, Tom is the first argument
and Jerry is the second argument.
Ans:When a class inherits from more than class, it will lead to inheritance
path amibiguity (this is normally calledthe diamond problem). Say A is the
super class of B and C & D is a subclass of both B and C. D inherits
properties of A from two different inheritance paths ie via both B & C. This
leads to ambiguity and related problems, so multiple inheritance is not
allowed in Java.
Q: What is the difference between static and non-static variables?
Or
Or
Or
Ans: A static variable is associated with the class as a whole rather than
with specific instances of a class. Each object will share a common copy of
the static variables i.e. there is only one copy per class, no matter how
many objects are created from it. Class variables or static variables are
declared with the static keyword in a class. These are declared outside a
class and stored in static memory. Class variables are mostly used for
constants. Static variables are always called by the class name. This
variable is created when the program starts and gets destroyed when the
programs stops. The scope of the class variable is same an instance
variable. Its initial value is same as instance variable and gets a default
value when its not initialized corresponding to the data type. Similarly, a
static method is a method that belongs to the class rather than any object
6
of the class and doesn’t apply to an object or even require that any
objects of the class have been instantiated.
Static methods are implicitly final, because overriding is done based on
the type of the object, and static methods are attached to a class, not an
object. A static method in a superclass can be shadowed by another static
method in a subclass, as long as the original method was not declared
final. However, you can’t override a static method with a non-static
method. In other words, you can’t change a static method into an
instance method in a subclass.
Ans:“this” is used to refer the currently executing object and it’s state.
“this” is also used for chaining constructors and methods.
What is a package?
7
Ans:A final variable's value can't be changed. final variables should be
initialized before using them.
Ans:It simply returns the absolute value of the value supplied to the
method, i.e. gives you the same value. If you supply negative value it
simply removes the sign.
Ans: method returns always double, which is not less than the supplied
value. It returns next available whole number
Ans: method returns always double, which is not greater than the
supplied value.
Ans:The min() method returns smaller value out of the supplied values.
Or
8
Or
Or
Or
Ans:”== “is used to check whether the references are of the same object.
.equals() is used to check whether the contents of the objects are the
same.
But with respect to strings, object refernce with same content
will refer to the same object.
String str1="Hello";
String str2="Hello";
If you take the same example with Stringbuffer, the results would be
different.
Stringbuffer str1="Hello";
Stringbuffer str2="Hello";
str1.equals(str2) will be true.
str1==str2 will be false.
9
Ans:Priority is thread ranking. Some threads can either run for a longer
timeslice or run more often (depending on the operating system). Peers
(or equals) get the same time/number of runs. Higher priority threads
interrupt lower priority ones. Priority is set from constants MIN_PRIORITY
(currently 1) to MAX_PRIORITY (currently 10) using the setPriority(int)
method. NORM_PRIORITY is the midrange value (currently 5). These are
defined in the Thread class.
What are the two ways to create a new thread?
Ans: a)Extend the Thread class and override the run() method.
b)Implement the Runnable interface and implement the run() method.
Ans:When a Thread calls the sleep() method, it will return to its waiting
state. When a Thread calls the yield() method, it returns to the ready
state.
Ans:The thread's start() method puts the thread in ready state and makes
the thread eligible to run. start() method automatically calls the run ()
method.
What is a Monitor?
10
What are two steps in Garbage Collection?
What is an Applet?
11
Q:State the significance of public, private, protected, default
modifiers both singly and in combination and state the effect of
package relationships on declared items qualified by these
modifiers.
Ans:Static means one per class, not one for each object no matter how
many instance of a class might exist. This means that you can use them
without creating an instance of a class.Static methods are implicitly final,
because overriding is done based on the type of the object, and static
methods are attached to a class, not an object. A static method in a
superclass can be shadowed by another static method in a subclass, as
long as the original method was not declared final. However, you can't
override a static method with a nonstatic method. In other words, you
can't change a static method into an instance method in a subclass.
Ans:A final class can't be extended ie., final class may not be subclassed.
A final method can't be overridden when its class is inherited. You can't
change value of a final variable (is a constant).
12
may not even know that the exception could be thrown. eg,
StringIndexOutOfBoundsException thrown by String's charAt() method·
Checked exceptions must be caught at compile time. Runtime exceptions
do not need to be. Errors often cannot be.
What is Overriding?
Ans:When a class defines a method using the same name, return type,
and arguments as a method in its superclass, the method in the class
overrides the method in the superclass.
When the method is invoked for an object of the class, it is the new
definition of the method that is called, and not the method definition from
superclass. Methods may be overridden to be more public, not more
private.
want to print "Hello" even before main is executed. How will you
acheive that?
Ans:Print the statement inside a static block of code. Static blocks get
executed when the class gets loaded into the memory and even before
the creation of an object. Hence it will be executed before the main
method.
Member classes - Member inner classes are just like other member
methods and member variables and access to the member class is
restricted, just like methods and variables. This means a public member
class acts similarly to a nested top-level class. The primary difference
between member classes and nested top-level classes is that member
classes have access to the specific instance of the enclosing class.
Local classes - Local classes are like local variables, specific to a block of
code. Their visibility is only within the block of their declaration. In order
for the class to be useful beyond the declaration block, it would need to
13
implement a
more publicly available interface.Because local classes are not members,
the modifiers public, protected, private, and static are not usable.
Ans: throw is used to explicitly raise a exception within the program, the
statement would be throw new Exception(); throws clause is used to
indicate the exceptions that are not handled by the method. It must
specify this behavior so the callers of the method can guard against the
exceptions. throws is specified in the method signature. If multiple
exceptions are not handled, then they are separated by a comma. the
statement would be as follows: public void doSomething() throws
14
IOException,MyException{}
If I write System.exit (0); at the end of the try block, will the
finally block still execute?
Ans:No in this case the finally block will not execute because when you
say System.exit (0); the control immediately goes out of the program,
and thus finally never executes.
What is Synchronization?
15
statements are similar to synchronized methods. A synchronized
statement can only be executed after a thread has acquired the lock for
the object or class referenced in the synchronized statement.
Does garbage collection guarantee that a program will not run out
of memory?
Ans:Garbage collection does not guarantee that a program will not run
out of memory. It is possible for programs to use up memory resources
faster than they are garbage collected. It is also possible for programs to
create objects that are not subject to garbage collection
.
What is the difference between paint(), repaint() and update()
methods within an applet which contains images?
Ans:paint : is only called when the applet is displayed for the first time, or
when part of the applet window has to be redisplayed after it was hidden.
repaint : is used to display the next image in a continuous loop by calling
the update method. update : you should be aware that, if you do not
implement it yourself, there is a standard update method that does the
following : it will reset the applet window to the current background color
(i.e. it will erase the current image) it will call paint to construct the new
image
16
Explain Enumeration Interface.
What is JDBC?
How many types of JDBC Drivers are present and what are they?
17
Ans:There are 4 types of JDBC Drivers Type 1: JDBC-ODBC Bridge Driver
Type 2: Native API Partly Java Driver Type 3: Network protocol Driver
Type 4: JDBC Net pure Java Driver
stmt.exceuteUpdate();
18
they appear. The first catch clause that is capable of handling the
exceptionis executed. The remaining catch clauses are ignored.
Ans:All tasks must implement the run() method, whether they are a
subclass of Thread or implement the Runnable interface.
19
Ans:It will print 13.
Ans:If the array is an array of primitive types, then all the elements of the
array will be initialized to the default value corresponding to that primitive
type. e.g. All the elements of an array of int will be initialized to 0, while
that of boolean type will be initialized to false. Whereas if the array is an
array of references (of any type), all the elements will be initialized to
null.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=10;
clrscr();
printf("Values before swaping are:");
printf("a=%d b=%d",a,b);
20
a=a+b;
b=a-b;
a=a-b;
printf("\nValues after swaping are:");
printf("a=%d b=%d",a,b);
getch();
}
Output:
#include<iostream.h>
#include<conio.h>
void main()
{
int a=5,b=10;
clrscr();
cout<<"Values before swaping are:";
cout<<"a="<<a<<" b="<<b;
a=a+b;
21
b=a-b;
a=a-b;
cout<<"\nValues after swaping are:";
cout<<"a="<<a<<" b="<<b;
getch();
}
Output:
class Swap
{
public static void main (String[] args)
{
int a=5,b=10;
System.out.println("Value before swaping are:");
System.out.println("a="+a+" b="+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("Value after swaping are:");
22
System.out.println("a="+a+" b="+b);
}
}
Output:
using System;
class Swap
{
public static void Main(string []args)
{
int a=5,b=10;
Console.WriteLine("Values before swaping are:");
Console.WriteLine("a="+a+" b="+b);
a=a+b;
b=a-b;
23
a=a-b;
Console.WriteLine("Values after swaping are:");
Console.WriteLine("a="+a+" b="+b);
}
}
Output:
INDEX
Topic Remarks
What is a Class?
What is an Object?
What is an Instance?
What do you mean by Patform independence?
24
How does Java acheive platform independence?
What is a destructor?
25
Why Java doesn’t support muliple inheritance?
Or
What is a package?
What is an Applet?
26
What is the difference between an Applet and a Java Application?
What is Synchronization?
What is JDBC?
How many types of JDBC Drivers are present and what are they?
Comprehensive VIVA-VOICE
For
JAVA
27
Topic JAVA TERMS
28