Java Interview Question and Answer SET 2
1. If you access an uninitialized local variable will result?
Syntax
Error
Compile
Time Error
Run Time
Error
No Error
2. Which is right way to declare Array?
int[]
myArray;
int
myArray[];
int []myArray;
A and B
Both
3. Overloaded methods are differentiated by
Number of
arguments
Data type
of arguments
Number
and the Data type of the arguments
None of
the above
4. A constructor that is automatically generated in the absence of
explicit constructors called?
Default
Constructor
Nullary
Constructor
Empty
Constructor
All the
above
5. Which package does define String and String Buffer classes?
6. Which of these is supported by method overriding in Java?
a)
Abstraction
b)
Encapsulation
c) Polymorphism
d) None
of the mentioned
7. Which of these keywords can be used to prevent Method overriding?
a) static
b)
Constant
c)
Protected
d)
final
8. What is the process of defining a method in subclass having same
name & type signature as a method in its super class?
a) Method
overloading
b) Method
overriding
c) Method
hiding
d)
None of the mentioned
9. Which of these is correct way of calling a constructor having no
parameters, of super class A by subclass B?
a) super
(void);
b)
superclass.();
c)
super.A();
d)
super();
10.
What are the basic features of java?
• Java is simple.
• Java provides immense security.
• Java provides high portability.
• Java provides object oriented programming features
• Java provides robustness.
• Java is Multithreaded.
• Java provides architecture neutrality.
• Java is distributed
• Java is dynamic.
• Java provides immense security.
• Java provides high portability.
• Java provides object oriented programming features
• Java provides robustness.
• Java is Multithreaded.
• Java provides architecture neutrality.
• Java is distributed
• Java is dynamic.
11. What is a variable in Java program?
• It's a memory location.
• The memory location is given some name.
• The memory location is being assigned some value.
• The value may change of the variable.
• The memory location size changes with the type of the
variable.
12. How a Java program compiles?
• First the source file name must be
extended with .java extension. e.g. Myprog.java
• Execute the javac compiler.
• javac compiler creates a file called
Myprog.class i.e. the bytecode version of Myprog.java.
• The bytecode is executed by the
Java runtime-systems which is called Java Virtual Machine (JVM).
• JVM is platform dependent.
13. What is an Object and how do you allocate memory to it?
Ans:
Object is an instance of a class and it is a software unit that
combines a structured set of data with a set of operations for inspecting and
manipulating that data. When an object is created using new operator, memory is
allocated to it.
14. What is the difference between constructor and method?
Ans:
Constructor will be automatically invoked when an object is
created whereas method has to be called explicitly.
15. What are Class, Constructor and Primitive data types?
Ans:
Class is a template for multiple objects with similar features
and it is a blue print for objects. It defines a type of object according to
the data the object can hold and the operations the object can perform.
Constructor is a special kind of method that determines how an
object is initialized when created.
Primitive data types are 8 types and they are: byte, short, int,
long, float, double, Boolean, char
16. Why String is immutable in Java?
17. Difference between equals () method and == operator?
Main difference between '==' and equals () in Java is that '=='
is an operator used to check whether two different reference refers to same
instance and equals () is a method used to check equality of an object. Another
important difference is '==' operators is used more with primitive data type
while equals () method is used for object.
18. What if the main () method is declared as private?
The program compiles properly but at run time it will give
"main () method not public." message.
19. What if the static modifier is removed from the signature
of the main () method?
Program compiles. But at run time throws an error
"NoSuchMethodError"
20. What environment variables do I need to set on my machine
in order to be able to run Java programs?
CLASSPATH and PATH are the two variables.
21. Can I have multiple main () methods in the same class?
No the program fails to compile. The compiler says that the main
() method is already defined in the class.
22. Do I need to import java.lang package any time? Why?
No. It is by default loaded internally by the JVM
23. What is Static Binding in Java ?
When type of object is determined at compile time(by the
compiler),its known as static binding there is any private,static or final
method in a class,its call static binding.
class Dog{
private void eat()
{
System.out.println("dog
is eating...");
}
public static void
main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}
24. Dynamic Binding or late binding :-
When type
of object is determined at run-time,its call dynamic binding.
class Animal{
void eat()
{
System.out.println("animal
is eating...");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("dog
is eating...");
}
public static void
main(String args[]){
Animal a=new Dog();
a.eat();
}
}
Output :- dog is eating
25. The difference between instance and local variables :-
1. Instance variables are
declared inside a class but not within a method.
class Horse {
private double height =
15.2;
private String breed;
// more code...
}
2. Local variables are
declared within a method.
class AddThing {
int a;
int b = 12;
public int add() {
int total = a + b;
return total;
}
}
3. Local variables MUST be initialized before use!
Local variables do NOT get a default value! The compiler
complains if you try to use a local variable before the variable is
initialized.
Instance variables are given default values, ie null if it's an
object reference, 0 if it's an int.
26. Does the method
change the instance variable balance ?
class CheckingAccount
{
. . . .
private int balance;
. . . .
public void
processDeposit( int amount )
{
int balance = 0; // New declaration of
balance.
this.balance = balance
+ amount ; // ??????
}
}
27. What is the wrong
with the below code. Is there any syntax error.
class CheckingAccount
{
. . . .
private int balance; //
Instance Variable
. . . .
public void
processDeposit( int amount )
{
int balance = 0; // New declaration of balance.
balance = balance + amount
; // This uses the local variable,
balance.
}
}
Comments
Post a Comment