HCL interview Questions Part 5
Is Empty .java file a valid source
file?
Can
a .java file contain more than one java classes?
Is
String a primitive data type in Java?
Is
main a keyword in Java?
Is
next a keyword in Java?
Is
delete a keyword in Java?
What happens if you dont initialize
an instance variable of any of the primitive types in Java?
What
will be the initial value of an object reference which is defined as an
instance variable?
What
are the different scopes for Java variables?
What
is the default value of the local variables?
How
many objects are created in the following piece of code?
MyClass c1, c2, c3;
c1 = new MyClass ();
c3 = new MyClass ();
Can
a public class MyClass be defined in a source file named YourClass.java?
Can
main method be declared final?
What
will be the output of the following statement?
System.out.println ("1" +
3);
What
will be the default values of all the elements of an array defined as an
instance variable?
Is Empty .java file a valid source
file?
Yes, an
empty .java file is a perfectly valid source file.
[capptitudebank.blogspot.com]
Can a .java file contain more than
one java classes?
Yes, a
.java file contain more than one java classes, provided at the most one of them
is a public class.
[capptitudebank.blogspot.com]
Is String a primitive data type in
Java?
No String
is not a primitive data type in Java, even though it is one of the most
extensively used object. Strings in Java are instances of String class defined
in java.lang package.
[capptitudebank.blogspot.com]
Is main a keyword in Java?
No, main is
not a keyword in Java.
[capptitudebank.blogspot.com]
Is next a
keyword in Java?
No, next is not a keyword.
[capptitudebank.blogspot.com]
Is delete a keyword in Java?
No, delete
is not a keyword in Java. Java does not make use of explicit destructors the
way C++ does.
[capptitudebank.blogspot.com]
Is exit a keyword in Java?
No. To exit
a program explicitly you use exit method in System object.
[capptitudebank.blogspot.com]
What happens if you dont initialize
an instance variable of any of the primitive types in Java?
Java by
default initializes it to the default value for that primitive type. Thus an
int will be initialized to 0, a boolean will be initialized to false.
[capptitudebank.blogspot.com]
What will be the initial value of an
object reference which is defined as an instance variable?
The object
references are all initialized to null in Java. However in order to do anything
useful with these references, you must set them to a valid object, else you
will get NullPointerExceptions everywhere you try to use such default
initialized references.
[capptitudebank.blogspot.com]
What are the different scopes for
Java variables?
The scope
of a Java variable is determined by the context in which the variable is
declared. Thus a java variable can have one of the three scopes at any given
point in time.
1. Instance : - These are typical object level variables,
they are initialized to default values at the time of creation of object, and
remain accessible as long as the object accessible.
2. Local : - These are the variables that are defined within
a method. They remain accessbile only during the course of method excecution.
When the method finishes execution, these variables fall out of scope.
3. Static: - These are the class level variables. They are
initialized when the class is loaded in JVM for the first time and remain there
as long as the class remains loaded. They are not tied to any particular object
instance.
[capptitudebank.blogspot.com]
What is the default value of the
local variables?
The local
variables are not initialized to any default value, neither primitives nor
object references. If you try to use these variables without initializing them
explicitly, the java compiler will not compile the code. It will complain abt
the local varaible not being initilized..
[capptitudebank.blogspot.com]
How many objects are created in the
following piece of code?
MyClass c1, c2, c3;
c1 = new MyClass ();
c3 = new MyClass ();
Only 2
objects are created, c1 and c3. The reference c2 is only declared and not
initialized.
[capptitudebank.blogspot.com]
Can a public class MyClass be
defined in a source file named YourClass.java?
No the
source file name, if it contains a public class, must be the same as the public
class name itself with a .java extension.
[capptitudebank.blogspot.com]
Can main method be declared final?
Yes, the
main method can be declared final, in addition to being public static.
[ Received
fromSandesh Sadhale]
What will be the output of the
following statement?
System.out.println ("1" + 3);
It will
print 13.
[capptitudebank.blogspot.com]
What will be the default values of
all the elements of an array defined as an instance variable?
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.
[capptitudebank.blogspot.com]
If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!
0 comments:
Have any question? Feel Free To Post Below: