Important Questions and Answers ON Strings, Methods, Method Overloading, Constructors AND Static Keyword
Important Questions and Answers ON Strings, Methods, Method Overloading, Constructors AND Static Keyword
KODNEST
TECHNOLOGIES
BENGALURU
1
KODNEST
1. What is a String?
( String is a series of characters, which is enclosed within double quotes
and is treated as an object in Java. )
2
KODNEST
15.How is it possible for two string objects with identical values not to be
equal under == operator?
(If one string is in constant pool and another string is in non constant
pool or if both strings are created using new keyword i.e both strings are
in non constant pool, then both string values can be same but both
string objects will have different references due to which both strings
will not be equal under == operator as == operator checks for string
references. )
4
KODNEST
class Test
{
public static void main(String[] args)
{
String s="KODNEST";
String s1=new String("KODNEST");
System.out.println("s==s1 "+(s==s1));
System.out.println("s.equals(s1)"+(s.equals(s1)));
}
}
( Output : s==s1 false
s.equals(s1)true )
Output: RamuS )
Output: Ramu10 )
Output: Ramu987.98 )
25. What are the three ways of creating an immutable String object?
( The immutable string object can be created in following three ways:
1. String s = “Omkar”;
2. String s = new String(“Omkar”);
3. char[ ] arr = {‘O’,‘M’,‘K’,‘A’,‘R’}
String s = new String(arr); )
7
KODNEST
The above statement would display a substring from “beginIndex”
character to “endIndex-1” character. )
44. Can we explicitly put a string object which is in the non-constant pool
into the constant pool?
( Using intern( ) a string object can be transferred from the non constant
pool to constant pool. )
9
KODNEST
10
KODNEST
12
KODNEST
12.Where are the reference variables and local variables created in java?
( The local variables and the reference variables are created in the
stack segment inside the activation record. )
13
KODNEST
17.Does the return type of the method play any role in method
overloading?
(No, the return type of the method does not play any role in method
overloading.)
14
KODNEST
1. Can we have two constructors with the same name, same number of
arguments and same type of arguments?
(Yes, we can have two constructors with the same name, same
number of arguments and same type of arguments provided the
order of datatype of arguments is different. )
4. When does the no argument constructor get inserted into our class?
(If our class does not contain any other constructor
then the no argument constructor will get inserted in our class.)
15
KODNEST
7. Can we use access modifier in a constructor declaration?
(Yes, we can use public, private etc..)
16
KODNEST
15.Does the compiler include default constructor if the class already
contains a user defined constructor?
(No, compiler will attach default constructor only if user defined
constructor is not present. )
17
KODNEST
18
KODNEST
26. When we create a class such as
class Kodnest
{
}
what extra additions would be performed by the compiler
automatically?
19
KODNEST
20
KODNEST
1. What are the different types of static elements that can exist in a class?
(There are three type of static elements i.e. static variable, static
block and static method. )
21
KODNEST
11. Can non static method access non static data? Why ?
(Yes, non static method can access the non static data. Because while
executing the non static method, memory for non static data would have
been allocated. )
22
KODNEST
19.From where would the static methods be loaded into the stack?
(From the static segment, static methods will be loaded.)
20. From where would the non static methods be loaded into the stack?
(From the code segment, non static methods will be loaded.)
23
KODNEST
24. I want to print "Hello" even before main() is executed. How can I
acheive that?
(If any set of statements should be executed before main method gets
executed then those set of statements should be kept inside the static
block.
Ex:
class Kodnest
{
public static void main(String []args)
{
System.out.println(“Inside main”);
}
static
{
System.out.println(“Hello”);
}
}
24
KODNEST
Output:- Hello
Inside main
25. Can we have a static variable and a non static variable with the same
name in a given class?
(No, variable names cannot be same.)
31.What is the error that we get if the main method is not declared as
static?
( Main method is not static in class ClassName, please define the main
method as:
25
KODNEST
public static void main(String[] args) )
33. If I do not provide any arguments on the command line, then the
String array of main() method will be empty or null?
( If no arguments are provided on command line them the String array
of main method will be empty. )
34. How many static initializers can you have and in what order do they
get executed?
( We can have any number of static initializers (static blocks).they
would get executed in the same order in which they are created. )
35. What is the error that we get if the main method is declared as private?
( Main method not found in class ClassName, please define the main
method as:
public static void main(String[] args) )
26
KODNEST
40. Can we apply static keyword on blocks?
(Yes, static keyword can be applied on blocks. )
45. How can we count the number of objects that are created of a class?
(Refer class notes)
27
KODNEST
50. What are static initializers?
( Static blocks are also called as static initializers. )
28