[go: up one dir, main page]

0% found this document useful (0 votes)
81 views28 pages

Important Questions and Answers ON Strings, Methods, Method Overloading, Constructors AND Static Keyword

This document discusses Strings, methods, method overloading, constructors, and the static keyword in Java. It provides 44 questions and answers on these topics. Some key points covered include: - A String is a series of characters treated as an object in Java. Individual characters cannot be accessed directly but can be accessed indirectly using charAt(). - There are mutable and immutable String types in Java. Immutable Strings are created in the constant pool or using new String(), while mutable Strings use StringBuffer and StringBuilder. - Method overloading is achieved by having different parameter types or counts in method signatures to allow for polymorphism. Constructors are used to initialize objects. - Common String methods include
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views28 pages

Important Questions and Answers ON Strings, Methods, Method Overloading, Constructors AND Static Keyword

This document discusses Strings, methods, method overloading, constructors, and the static keyword in Java. It provides 44 questions and answers on these topics. Some key points covered include: - A String is a series of characters treated as an object in Java. Individual characters cannot be accessed directly but can be accessed indirectly using charAt(). - There are mutable and immutable String types in Java. Immutable Strings are created in the constant pool or using new String(), while mutable Strings use StringBuffer and StringBuilder. - Method overloading is achieved by having different parameter types or counts in method signatures to allow for polymorphism. Constructors are used to initialize objects. - Common String methods include
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

KODNEST

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. Is String an object in Java?


( Yes, String is treated as object in Java. )

3. Can we access individual character in String?


( No, individual character in String cannot be accessed directly. i.e.
Ex: String s = “KODNEST”;
System.out.println(s[2]);
Output:- ERROR

But we can access the individual character in String indirectly using


charAt( ). i.e.
Ex: String s = “KODNEST”;
System.out.println(s.charAt(2));
Output:- D )

4. Does a Java String end with a null character?


( No, Java Strings are not ended with a null character. )

5. What are the types of String in Java?


( In Java, there are two types of Strings:
1. Mutable String
2. Immutable String )

2
KODNEST

6. What are the two ways of creating immutable strings?


( String s=”OMKAR”;
String s=new String(“OMKAR”); )

7. How are mutable strings created in Java?


(The mutable strings can be created using StringBuffer and
StringBuilder inbuilt classes.)

8. Where are immutable strings created in Java?


( In Java, the immutable strings are created in the heap Segment of JRE. )

9. What is a constant pool?


( Constant pool is a region of the heap segment on the ram where
memory is allocated for such strings that are created without using the
new operator. Duplicates are not permitted in this region. )

10.What is non constant pool?


( Non constant pool is a region of the heap segment where memory is
allocated for such strings which are created using the new operator or
using expressions. Duplicates are permitted in this region. )

11. What is the role of equals() in Java?


( In Java, equals( ) is used to compare two strings values. )

12.What is the role of == operator in Java?


( In Java, ==operator is used to compare two references of a string. )

13.Name some of the commonly used in built methods of String class?


( Few inbuilt methods of String class are as follows:
1. startsWith( )
2. endsWith( )
3. toUpperCase( )
3
KODNEST
4. toLowerCase( )
5. contains( )
6. subString( )
7. toCharArray( ) )

14.How do you compare two Strings in Java?


(In Java, comparison of two can be done in following ways:
1. By using equals( )
2. By using compareTo( )
3. By using == operator
4. By using equalsIgnoreCase( ) )

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. )

16.Explain the difference between the following statements?


String s=”PRAHLLAD OMKAR BHARADWAJ”;
String s= new String(”PRAHLLAD OMKAR BHARADWAJ”);
( The first string would be created in constant pool region.
The second String would be created in the non-constant pool region. )

17.How to concatenate two different Strings?


( Strings can be concatenated in following three ways:
i) Using concat( )
ii) Using append( )
iii) Using “+” operator. )

4
KODNEST

18.What is the output of the following program?

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 )

19.Is null a keyword in java?


(In Java, null is a reserved word (keyword) for literal values. It seems
like a keyword, but actually, it is a literal similar to true and false. )

20. In which package is the String class present?


(The String class is present in java.lang package. )

21.What is a String literal?


( The fixed value enclosed within “ ” or a series of character enclosed
within “ ” is known as String literal.
Ex: “OMKAR” )

22. What happens when you add a char value to a String?


( The char value would be converted to a string and would be
concatenated with the existing string.

Ex: String s=”Ramu”;


5
KODNEST
System.out.println(s+’S’);

Output: RamuS )

23. What happens when you add a int value to a String?


( The int value would be converted to a string and would be
concatenated with the existing string.

Ex: String s=”Ramu”;


System.out.println(s+10);

Output: Ramu10 )

24. What happens when you add a double value to a String?


( The double value would be converted to a string and would be
concatenated with the existing string.

Ex: String s=”Ramu”;


System.out.println(s+987.98);

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); )

26. In how many ways can String comparison be performed?


(In Java, comparison of Strings can be performed in following ways:
1.By using equals( )
2. By using compareTo( )
6
KODNEST
3. By using == operator
4. By using equalsIgnoreCase( ) )

27. What is the role of equals()?


( It compares two strings values by considering case sensitivity. )

28. What is the role of equalsIgnoreCase()?


( It compares two strings values by ignoring case sensitivity. )

29. What is the role of length()?


( It is used to return the length of the string. )

30. What is the role of substring()?


( substring( ) is used for getting a substring of a particular String. )

31. Explain the two versions of substring()?


( There are two versions of substring() method:
1. String substring(int beginIndex):

Ex: String s = “Omkar”;


System.out.println(s.substring(2));
Output: kar

The above statement would display a substring from “beginIndex”


character onwards till the end of the String.

2. String substring(int beginIndex, int endIndex):

Ex: String s = “Omkar”;


System.out.println(s.substring(2,4));
Output: ka

7
KODNEST
The above statement would display a substring from “beginIndex”
character to “endIndex-1” character. )

32. What is the role of toLowerCase()?


( It returns a string by converting it into lower case. )

33. What is the role of toUpperCase()?


( It returns a string by converting it into upper case. )

34. What is the role of contains()?


( It is used to verify that if a string contains a particular substring or
not. )

35. What is the role of concat()?


( The concat() method is used to concatenate or combine two strings. )

36. What is the role of isEmpty()?


( It is used to check whether the string is empty or not. )

37. What is the role of indexOf()?


( It returns the index of a specified character within a string. )

38. What is the default buffer size of the StringBuffer class?


( The default buffer size of StringBuffer class is 16. )

39. What is the role of append()?


(It is used to concatenate or combine two mutable strings.)

40. What is the role of capacity()?


( It returns the capacity of StringBuffer or StringBuilder.)

41.What is the role of intern()?


(It brings the copy of a string from non constant pool to constant pool)
8
KODNEST

42. What is the role of StringTokenizer class?


(It is used to chop a large string at a specified character and provide
tokens.)

43. In which package is the StringTokenizer class present?


( The StringTokenizer class is present in java.util package )

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

1. Which object oriented concept is achieved using overloading?


( Polymorphism (Virtual Polymorphism) concept would be achieved
using method overloading. )

2. What restrictions are placed on method overloading?


( The restrictions in the overloaded methods are:
i) The number of parameters should be different. Or,
ii) The data types of the parameters should be different. Or,
iii) The order of the data types of the parameters should be
different. )

3. Does c++ support method overloading?


( Yes, C++ supports method overloading. )

4. Can overloaded methods have different return types?


( Yes, overloaded methods can have different return types. )

5. Is method overloading compile time polymorphism or run time


polymorphism?
( Method overloading is compile time polymorphism. )

6. What are methods in java also called as?


(Methods in Java can be called as functions, modules, subroutines,
procedures, does-part, behaviors. )

10
KODNEST

7. What are the 4 different types of behaviors in java?


( The 4 different types of behaviors are as follows:

1. Type-I methods: The methods which would not accept any


parameter and would not return any value.

2. Type-II methods: The methods which would accept the parameters


and would not return any value.

3. Type-III methods: The methods which would not accept parameter


but would return a value.

4. Type-IV methods: The methods which would accept parameter and


would return a value. )

8. Give examples of 4 types of behaviors in java.


( 1) Type-I methods: The methods which would not accept any
parameter and would not return any value.
Ex: void add( )
{
int a = 10;
int b = 20;
int c = a+b;
System.out.println(c);
}

2) Type-II methods: The methods which would accept the parameters


and would not return any value.
Ex: void add( int a, int b )
{
int c = a+b;
System.out.println(c);
11
KODNEST
}

3) Type-III methods: The methods which would not accept parameter


but would return a value.
Ex: int add( )
{
int a = 10;
int b = 20;
int c = a+b;
return c;
}

4) Type-IV methods: The methods which would accept parameter and


would return a value.

Ex: int add( int a, int b )


{
int c = a+b;
return c;
}
)

9. What is an activation record?


(Activation record is a record which is created on the stack segment
whenever a method is called or control enters the method. It would
get deleted whenever control leaves the method. )

10.When and where is the activation record created?


( Activation record is created on the stack, when the method is
called. )

12
KODNEST

11. When does the activation record get deleted?


( When control goes out of the method or when control leaves the
method, activation record gets deleted. )

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.What is meant by method overloading?


( Method overloading is the process of creating multiple methods
with the same name. It is used to achieve virtual polymorphism in
java. )

14.What is overloaded in method overloading?


(Nothing is overloaded in method overloading. User of the methods
virtually feels one method is doing multiple activities , but in reality
there would be multiple methods all with the same name doing one
one activity each. )

15.What is the criteria that must be ensured for method overloading in


java?
( For method overloading below criteria must be ensured:
i) The number of parameters should be different. Or,
ii) The data types of the parameters should be different. Or,
iii) The order of the data types of the parameters should be
different. )

16.What is the advantage of method overloading?


(The advantage of method overloading is that it increases the ease of
usability and user is prevented from memorizing multiple method
names. )

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. )

2. What happens if we don’t explicitly provide a constructor in our class?


(The Java compiler will insert a default constructor or zero
parameterized constructor in the class. )

3. Who inserts a no argument constructor into our class?


(The no argument constructor or zero parameterized constructor is
inserted by the java compiler.)

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.)

5. What is the no argument constructor also called as?


(The no argument constructor is also called as default constructor
or zero parameterized constructor. )

6. Whom does the no argument constructor of our class implicitly call?


(The no argument constructor implicitly call the Object class
constructor.)

15
KODNEST
7. Can we use access modifier in a constructor declaration?
(Yes, we can use public, private etc..)

8. What is the difference between parameters and arguments?


( Arguments - At the time of method call the data sent to a
method is known as argument. It is also known as actual parameters.

Parameters - In the method definition, the variables present to


accept the arguments is known as parameters. It is also known as
formal parameters. )

9. When does a parameter shadow the class field?


(When the parameter name and instance variables name are same.)

10.How can shadowing problem be resolved?


(The shadowing problem can be resolved using this keyword.)

11. Is it compulsory to have a constructor within a class?


(Yes, but for programmer it is not compulsory. If programmers is
not keeping a constructor then the java compiler will attach a
constructor.)

12.Can we overload constructors?


(Yes, constructors can be overloaded. )

13.What is meant by constructor chaining?


(The process of calling parent class constructor from the child class
constructor using super( ) is called Constructor Chaining.)

14.What is the return type of a constructor?


(Constructors does not have any return type. )

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. )

16.How can local chaining of constructors be achieved?


(The local chaining can be achieved by using this() .)

17.Which construct must be used to achieve local chaining? What is its


limitation?
(this() should be used to achieve local chaining. The limitation of
this() method is that, in order to use this() method minimum two
constructors must be present within a class. )

18.Does every constructor call its superclass constructor?


(Yes, provided this() is not present in the constructor. )

19.When is an implied super() not included in each constructor?


(If this() is included in the constructor then super() will not be
included. )

20. Why is a constructor called a constructor?


( As constructor is used to create or construct the object and it is the
constructor that will be used to initialize the instance variables of
object, the name constructor is given as constructor. )

21.What is a role of default constructor?


(The default constructor within its body would have a call to the
super class constructor. The super class constructor would initialize
the instance variables to default values. )

17
KODNEST

22. What is the difference between constructor and ordinary


methods?

Constructors Ordinary methods


Has the same name as Does not have the same
that of the class. name as that of the class.
Has no return type Has return type
It is called during object It is called after object
creation. creation.
It is involved in It is involved in exhibiting
construction of an object. other behaviours.
super() and this() super() and this()
methods can be used only methods cannot be used
inside a constructors. within an ordinary
methods.

23. What are the operations that a constructor performs?


(It normally performs initialization i.e construction of an object. )

24. Can constructors be public?


(Yes, constructors can be public. )

25. Can constructors be private?


(Yes, constructors can be private.)

18
KODNEST
26. When we create a class such as
class Kodnest
{

}
what extra additions would be performed by the compiler
automatically?

class Kodnest extends Object


{
public Kodnest()
{
super();
}
}

27. How is constructor chaining achieved in java?


(The constructor chaining can be achieved using super(). )

28. How are this() and super() used with constructors?


(this() is used to achieve local chaining and super() is used to
achieve constructor chaining.)

29. What happens when a constructor is declared as private?


(It cannot be accessed from outside the class. )

30. What is a constructor in java?


(Constructor is a specialized setter which has the same name as
that of the class and does not have return type. )

19
KODNEST

31.When does a java compiler add a default constructor to a java


program?
(If the class does not contain any user defined constructor then
the default constructor will be added by the java compiler.)

32. Why do we require the overloaded constructors in Java?


( The objects that are created for a class may demand different types
of initialization.
Some objects may have to be initialized to their default values in
which case, default constructor is required.
Some objects may have to be initialized to different values as required
by the programmer for which parameterized constructors must be
used.
Therefore a class would normally contain overloaded constructor.
)

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. )

2. When should we declare a variable as static?


(When common copy of the value is required for all the objects of the
class, the variable should be declared as static. )

3. When should we create static blocks?


(To initialize static variables and to execute a set of statements even
before the execution of main method. )

4. When should we declare a variable as non static?


( When we require a separate values for each variables of an object,
then we should declare non static variables. )

5. When should we create non static blocks?


( To initialize non static variables and it also enables to count the
number of objects of a class. )

6. When should we declare a method as non static?


( If it is not a utility method or if it is a specific method or if it is such
a method which has to be accessed through a reference. )

21
KODNEST

7. Can static method access non static data? Why ?


( No. Because while executing static elements, memory might not
have been allocated for non static variables. )

8. Can static block access non static data? Why ?


( No. Because while executing static blocks, memory might not have
been allocated for non static variables. )

9. Can static method access static data? Why?


(Yes, static method can access the static data. Because while
executing the static method, memory for static data would have been
allocated.)

10.Can static block access static data? Why?


(Yes, static block can access the static data. Because while executing
the static block, memory for static data would have been allocated. )

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. )

12.Can non static block access non static data? Why ?


(Yes, non static block can access the non static data. Because while
executing the non static block, memory for non static data would have
been allocated. )

13.Can non static method access static data? Why?


(Yes, non static method can access the static data. Because while
executing the non static method, memory for static data would have been
allocated. )

22
KODNEST

14.Can non static block access static data? Why?


(Yes, non static block can access the static data. Because while
executing the non static block, memory for static data would have been
allocated. )

15.Who initializes static variable?


( JVM initializes the static variable. )

16.Who initializes non static data?


( Constructor initializes the non static data. )

17.When does the static block get executed?


( Static block get executed before main method gets executed. )

18.When does the non static block get executed?


(Non static block gets executed before constructor gets executed
during the object creation.)

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.)

21.What is a class loader?


( It is the part of JVM which loads all the static elements from code
segment onto the static segment. The roles of class loader are,
1. Loads all the static variables on static segment.
2. Loads static block on static segment.
3. Loads static methods from code segment on the static
segment. )

23
KODNEST

22. Why should the main() be declared static?


( If the main() method is not declared as static then class loader cannot
load the main() method into the static segment.
JVM after receiving the control from the class loader would
automatically searches for the main() method in the static segment. If it
is not present in static segment, then JVM cannot handover the control
to the main() method and load it onto the stack segment.
In other words the application never gets launched. )

23. List practical applications of static keyword?


1. Aadhar card-nationality
2. Students-university
3. Institute-fees

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.)

26. Can we use this keyword on a static variable?


(No, this keyword can be used only with instance variable.)

27. Can we use this keyword on a non static variable?


(Yes, this keyword can be used with non static or instance variable.)

28. Can we declare a static variable inside a static method?


( No, if static variables declared inside the method, then it would
become local variable and memory would be allocated inside the stack
segment but for static variables the memory should be allocated inside the
static segment. )

29. Can we declare a static variable inside a non-static method?


( No, if static variables declared inside the method, then it would
become local variable and memory would be allocated inside the stack
segment but for static variables the memory should be allocated inside the
static segment. )

30. What happens to a static variable that is defined within a method of a


class?
(The java compiler will not permit it and error will be shown. )

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) )

32. Can we declare static public void as public static void?


(Yes, static public void can be declared as public static void also.)

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) )

36. Can main() method be overloaded?


(Yes, main method can be overloaded in Java.)

37. What is the main role of static keyword?


(The main role of static keyword is that it helps in memory
management. )

38. Can we apply static keyword on variables?


(Yes, static keyword can be applied on variables. )

39. Can we apply static keyword on methods?


(Yes, static keyword can be applied on methods. )

26
KODNEST
40. Can we apply static keyword on blocks?
(Yes, static keyword can be applied on blocks. )

41.Can we apply static keyword on package?


(Yes, static keyword can be applied on package. )

42. Can we apply static keyword on constructor?


(No, static keyword cannot be applied on constructors. )

43. What is a static variable also known as?


(Static variable is also known as Class variable. )

44. What is a static method also known as?


(Static method is also known as utility method. )

45. How can we count the number of objects that are created of a class?
(Refer class notes)

46. Can we call a static method without using an object?


( Yes, static method can be called without using an object, by using the
class name. )

47. Can we call a static method using an object?


(Yes, static method can be called using an object as well as using the
class name.)

48. Can we call a non static method without using an object?


(No, non static method cannot be called without using an object. )

49. Can we call a non static method using an object?


(Yes, non static method can be called using an object. )

27
KODNEST
50. What are static initializers?
( Static blocks are also called as static initializers. )

51.When are non static variables initialized?


( When objects or instances are created. )

52. Can static methods be overloaded?


(Yes, static methods can be overloaded. )

53. How many arguments can be passed to the main()?


( As many as possible )

54. Give the alternate syntax of main()?


The alternative syntax of main method are as follows:
1. static public void main(String []args)
2. static public void main(String args[])
3. static public void main(String…args)

28

You might also like