0% found this document useful (0 votes)
6 views26 pages

Java Programming Concepts and Selenium Guide

The document covers various programming concepts, including differences between object-oriented and object-based programming languages, user-defined method overriding and overloading in Selenium, and Java fundamentals such as memory areas, access specifiers, constructors, and static methods. It also addresses specific Java questions and answers related to the language's features and behaviors. Additionally, it provides insights into the use of AutoItX for handling Windows pop-ups in Selenium tests.

Uploaded by

itscynthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views26 pages

Java Programming Concepts and Selenium Guide

The document covers various programming concepts, including differences between object-oriented and object-based programming languages, user-defined method overriding and overloading in Selenium, and Java fundamentals such as memory areas, access specifiers, constructors, and static methods. It also addresses specific Java questions and answers related to the language's features and behaviors. Additionally, it provides insights into the use of AutoItX for handling Windows pop-ups in Selenium tests.

Uploaded by

itscynthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

***Windows pop up handle???

AutoItX

File file = new File("lib", jacobDllVersionToUse);


[Link](LibraryLoader.JACOB_DLL_PATH, [Link]());
WebDriver driver = new FirefoxDriver();
[Link]("[Link]
WebElement printButton = [Link]([Link]("printButton"));
[Link]();
AutoItX x = new AutoItX();
[Link]("Print");
[Link]("Print");
[Link]("Print", "", "1058");
[Link]("Print", "", "1153", "50");
[Link](3000); //This was added just so you could see that the values did change.
[Link]("Print", "", "2");

***What is the diff bet object oriented program lang and object based prg lan???

Object oriented Programming language:


It supports all the features of OOPs(Abstraction, Inheritance, Polymorphism)

***All the characteristics and features of object oriented programming are supported.
***These types of programming languages don’t have a built in object
***Java is an example of object oriented programming language which supports creating and
inheriting (Which is reusing of code one class from another)

Object Based programming language:


It don’t support all the features of OOPs.

***All characteristics and features of object oriented programming, such as inheritance and
polymorphism are not supported.
***These types of programming languages have built-in objects. Ex: Java script has a window
object
***VB is another example of object based language as you can create and use classes and object,
but inheriting classes is not supported

***User defined overriding in selenium???


[Link] to create user-defined overriding methods?
*Create class-method-bl-same method with same arguments in different class.
*We need to create a base class in which we need to create our own method for example like
take screen shot. That we will use in our step definition testing execution page.
2. How to create user-defined overloading methods?
* Base class - sendkeys two methods and we use with different arguments.
*Based on argument counts we are changing the value.

3. How sendkeys acts as overloaded?


Normal [Link](“String”)
Actions: action class [Link] (webelement, “String”)

4. Where and all we get null pointer exception while using senkeys?
*While executing senkeys – for example if we use senkeys for password and while loading that
particular page it delayed and that particular field did not get opened. Then it will throw null
pointer exception.

5. How to take screen shot for 10 different pages?


*We need to create a base class for takescreen and while executing it we have to mention
wherever we want and need to change the name based on that.
*We can even use hooks concept for this.

[Link] java compiler converts source code to


Ans. Byte Code

2. Java is platform dependent or independent


Ans. independent

[Link] is responsible for converting byte code to machine code?


Ans. jvm

[Link] method is starting point of every java program?


Ans. main

[Link] main method takes arguments/parameters of which type?


Ans. array of Strings

[Link] is executed before the main method


Ans. static blocks

7.A constructor returns a value. (true or false)


Ans. false

[Link] method is used to refer to the current object in constructor chaining?


Ans. this

[Link] Casting happens automatically when we convert higher data type to a lower data type.
(true or false)
Ans. false

[Link] data type holds true and false values


Ans. Boolean

11. Which of the operators is used for short circuit operators?


Ans. Logical &&

12. Which of the operators can be used to concatenate strings?


Ans. +

[Link] is the result for


int i=10;
[Link](i++);
Ans. 10

[Link] of the looping constructs allow us to initialize,check the condition and increment or
decrement all in one line?
Ans. for loop

15. Which of the access Modifiers is used only with inheritance?


Ans. protected.

16. To have package level access we should use which of the modifier?
Ans. Default.

17. Fields marked private can be accessed only the method of that class. (true or false)
Ans. True

18. Which keyword is used to use classes from other packages


Ans. Import

19. Default package of java


Ans. [Link] package

20. [Link]() throws which exception?


Ans. interrupted exception
21. Which keyword is used to inherit a class?
Ans. extends

22. A class can extend multiple class. (true or false)


Ans. false

23. Can we define a main method in the abstract class. (true or false)
Ans. True

24. Can we define a final abstract class. (true or false)


Ans. False

25. What keyword is used to mark a class so that it cannot be extended?


Ans. Final

26. Method overloading implements static binding and compile time polymorphism. (true or
false)
Ans. True

27. Method overriding implements runtime polymorphism. (true or false)


Ans. True

28. Object down casting happens implicitly. (true or false)


Ans. False

29. A class can implement multiple interface. (true or false)


Ans. True

30. What is the parent class of all the exception classes?


Ans. Throwable

31. Can we use a finally block without a catch.(Yes or No)


Ans. Yes

32. Which method on a string class can be used to split the given string into multiple Strings
Ans. split()

33. StringBuffer is Immutable. (true or false)


Ans. False
44. Which method can be used to compare values of two String
Ans. equals method

45. Which method do you use to add an element to a Map?


Ans. put()

46. Selenium IDE is a plugin for which browser?


Ans. Firefox

47. To use the WebDriver, we must import which Jar file?


Ans. Selenium Stand-Alone Server

48. To type text into a textbox using selenium, we use the method called
Ans. sendKeys()

49. which is generally the prefered item locator method?(ID, Name, CSS Selector, xpath)
Ans. ID

50. Which JAR file is required to read an Excel file?


Ans. Poi-ooxml

51. Feature files contain a Maximum of one scenario.(true or false)


Ans. False

52. We pass the data in scenario through Examples tag. (true or false)
Ans. False

53. DataTable is used to pass bulk of data.(true or false)


Ans. False

54. DataTable is used to store bulk of data.(true or false)


Ans. True

55. Can we create object for Hard Assert. (Yes or No) If No then why?
Ans. No because it is static

6) How many types of memory areas are allocated by JVM?


Many types:
1. Class (Method) Area: Class Area stores per-class structures such as the runtime constant
pool, field, method data, and the code for methods.
2. Heap: It is the runtime data area in which the memory is allocated to the objects
3. Stack: Java Stack stores frames. It holds local variables and partial results, and plays a
part in method invocation and return. Each thread has a private JVM stack, created at the
same time as the thread. A new frame is created each time a method is invoked. A frame
is destroyed when its method invocation completes.
4. Program Counter Register: PC (program counter) register contains the address of the
Java virtual machine instruction currently being executed.
5. Native Method Stack: It contains all the native methods used in the application.
More Details.

7) What is JIT compiler?


Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the
bytecode that have similar functionality at the same time, and hence reduces the amount of time
needed for compilation. Here the term “compiler” refers to a translator from the instruction set of
a Java virtual machine (JVM) to the instruction set of a specific CPU.

9) What are the main differences between the Java platform and other platforms?
There are the following differences between the Java platform and other platforms.
o Java is the software-based platform whereas other platforms may be the hardware
platforms or software-based platforms.
o Java is executed on the top of other hardware platforms whereas other platforms can only
have the hardware components.
10. Why Java is not 100% object oriented?
* It is because it is supports primitive data type link byte,long,etc., which are not objects.
Because in java we use data types like int, float, double etc., which are not object oriented, and
of course is what opposite to OOPS.

11) What is classloader?


Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java
program, it is loaded first by the classloader. There are three built-in classloaders in Java.
1. Bootstrap ClassLoader: This is the first classloader which is the superclass of Extension
classloader. It loads the [Link] file which contains all class files of Java Standard Edition
like [Link] package classes, [Link] package classes, [Link] package classes, [Link]
package classes, [Link] package classes, etc.
2. Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader
of System classloader. It loads the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
3. System/Application ClassLoader: This is the child classloader of Extension classloader.
It loads the class files from the classpath. By default, the classpath is set to the current
directory. You can change the classpath using "-cp" or "-classpath" switch. It is also
known as Application classloader.

16) What is the default value of the local variables?


The local variables are not initialized to any default value, neither primitives nor object
references.

17) What are the various access specifiers in Java?


In Java, access specifiers are the keywords which are used to define the access scope of the
method, class, or a variable. In Java, there are four access specifiers given below.
o Public The classes, methods, or variables which are defined as public, can be accessed by
any class or method. It is global level access( same package + different package)
o Protected Protected can be accessed by the class of the same package, or by the sub-class
of this class, or within the same class. Inside package + outside Package (Extends)
Default Default are accessible within the package only. By default, all the classes,
methods, and variables are of default scope. Package level access & without access
specifier within the package we can access.
o Private The private class, methods, or variables defined as private can be accessed within
the class only. It is a class level access

18) What is the purpose of static methods and variables?


The methods or variables defined as static are shared among all the objects of the class. The
static is the part of the class and not of the object. The static variables are stored in the class area,
and we do not need to create the object to access such variables. Therefore, static is used in the
case, where we need to define variables or methods which are common to all the objects of the
class.
For example, In the class simulating the collection of the students in a college, the name of the
college is the common attribute to all the students. Therefore, the college name will be defined
as static.

19) What are the advantages of Packages in Java?


There are various advantages of defining packages in Java.
o Packages avoid the name clashes.
o The Package provides easier access control.
o We can also have the hidden classes that are not visible outside and used by the package.
o It is easier to locate the related classes.
24) What is an object?
The Object is the real-time entity having some state and behaviour. In Java, Object is an instance
of the class having the instance variables as the state of the object and the methods as the
behaviour of the object. The object of a class can be created by using the new keyword.

28) How many types of constructors are used in Java?


Based on the parameters passed in the constructors, there are two types of constructors in Java.
o Default Constructor: default constructor is the one which does not accept any value.
The default constructor is mainly used to initialize the instance variable with the default
values. It can also be used for performing some useful task on object creation. A default
constructor is invoked implicitly by the compiler if there is no constructor defined in the
class.
o Parameterized Constructor: The parameterized constructor is the one which can
initialize the instance variables with the given values. In other words, we can say that the
constructors which can accept the arguments are called parameterized constructors.

29) What is the purpose of a default constructor?


The purpose of the default constructor is to assign the default value to the objects. The java
compiler creates a default constructor implicitly if there is no constructor in the class.

31) Is constructor inherited?


No, the constructor is not inherited.

32) Can you make a constructor final?


No, the constructor can't be final.

33) Can we overload the constructors?


Yes, the constructors can be overloaded by changing the number of arguments accepted by the
constructor or by changing the data type of the parameters.

34) What do you understand by copy constructor in Java?


There is no copy constructor in java. However, we can copy the values from one object to
another like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They are:
o By constructor
o By assigning the values of one object into another
o By clone() method of Object class
35) What are the differences between the constructors and methods?

39) What is the static variable?


The static variable is used to refer to the common property of all objects (that is not unique for
each object), e.g., The company name of employees, college name of students, etc. Static
variable gets memory only once in the class area at the time of class loading. Using a static
variable makes your program more memory efficient (it saves memory). Static variable belongs
to the class rather than the object.

40) What is the static method?


o A static method belongs to the class rather than the object.
o There is no need to create the object to call the static methods.
o A static method can access and change the value of the static variable.
42) Why is the main method static?
Because the object is not required to call the static method. If we make the main method non-
static, JVM will have to create its object first and then call main() method which will lead to the
extra memory allocation. More Details.

43) Can we override the static methods?


No, we can't override static methods.

44) What is the static block?


Static block is used to initialize the static data member. It is executed before the main method, at
the time of classloading.

45) Can we execute a program without main() method?


Ans) Yes, one of the ways to execute the program without the main method is using static
block. More Details.

46) What if the static modifier is removed from the signature of the main method?
Program compiles. However, at runtime, It throws an error "NoSuchMethodError."

47) What is the difference between static (class) method and instance method?

static or class method instance method

1)A method that is declared as static is A method that is not declared as static is
known as the static method. known as the instance method.

2)We don't need to create the objects to call The object is required to call the instance
the static methods. methods.

3)Non-static (instance) members cannot be Static and non-static variables both can be
accessed in the static context (static accessed in instance methods.
method, static block, and static nested
class) directly.

4)For example: public static int cube(int n){ For example: public void msg(){...}.
return n*n*n;}

48) Can we make constructors static?


As we know that the static context (method, block, or variable) belongs to the class, not the
object. Since Constructors are invoked only when the object is created, there is no sense to make
the constructors static. However, if you try to do so, the compiler will show the compiler error
49) Can we make the abstract methods static in Java?
In Java, if we make the abstract methods static, It will become the part of the class, and we can
directly call it which is unnecessary. Calling an undefined method is completely useless therefore
it is not allowed.

50) Can we declare the static variables and methods in an abstract class?
Yes, we can declare static variables and methods in an abstract method. As we know that there is
no requirement to make the object to access the static context, therefore, we can access the static
context declared inside the abstract class by using the name of the abstract class.

51) What is this keyword in java?


The this keyword is a reference variable that refers to the current object. There are the various
uses of this keyword in Java. It can be used to refer to current class properties such as instance
methods, variable, constructors, etc. It can also be passed as an argument into the methods or
constructors. It can also be returned from the method as the current class instance.

52) What are the main uses of this keyword?


There are the following uses of this keyword.
o this can be used to refer to the current class instance variable.
o this can be used to invoke current class method (implicitly)
o this() can be used to invoke the current class constructor.
o this can be passed as an argument in the method call.
o this can be passed as an argument in the constructor call.
o this can be used to return the current class instance from the method.

53) Can we assign the reference to this variable?


No, this cannot be assigned to any value because it always points to the current class object and
this is the final reference in Java. However, if we try to do so, the compiler error will be shown.

55) How can constructor chaining be done using this keyword?


Constructor chaining enables us to call one constructor from another constructor of the class with
respect to the current class object. We can use this keyword to perform constructor chaining
within the same class. Consider the following example which illustrates how can we use this
keyword to achieve constructor chaining.

58) Why is Inheritance used in Java?


There are various advantages of using inheritance in Java that is given below.
o Inheritance provides code reusability. The derived class does not need to redefine the
method of base class unless it needs to provide the specific implementation of the
method.
o Runtime polymorphism cannot be achieved without using inheritance.
o We can simulate the inheritance of classes with the real-time objects which makes OOPs
more realistic.
o Inheritance provides data hiding. The base class can hide some data from the derived
class by making it private.
o Method overriding cannot be achieved without inheritance. By method overriding, we
can give a specific implementation of some basic method contained by the base class.

61) What is aggregation?


Aggregation can be defined as the relationship between two classes where the aggregate class
contains a reference to the class it owns. Aggregation is best described as a has-a relationship.
For example, The aggregate class Employee having various fields such as age, name, and salary
also contains an object of Address class having various fields such as Address-Line 1, City,
State, and pin-code. In other words, we can say that Employee (class) has an object of Address
class.

62) What is composition?


Holding the reference of a class within some other class is known as composition. When an
object contains the other object, if the contained object cannot exist without the existence of
container object, then it is called composition. In other words, we can say that composition is the
particular case of aggregation which represents a stronger relationship between two objects.
Example: A class contains students. A student cannot exist without a class. There exists
composition between class and students.

63) What is the difference between aggregation and composition?


Aggregation represents the weak relationship whereas composition represents the strong
relationship. For example, the bike has an indicator (aggregation), but the bike has an engine
(composition).

64) Why does Java not support pointers?


The pointer is a variable that refers to the memory address. They are not used in Java because
they are unsafe(unsecured) and complex to understand.

65) What is super in java?


The super keyword in Java is a reference variable that is used to refer to the immediate parent
class object. Whenever you create the instance of the subclass, an instance of the parent class is
created implicitly which is referred by super reference variable. The super() is called in the class
constructor implicitly by the compiler if there is no super or this.

67) What are the main uses of the super keyword?


There are the following uses of super keyword.
o super can be used to refer to the immediate parent class instance variable.
o super can be used to invoke the immediate parent class method.
o super() can be used to invoke immediate parent class constructor.

68) What are the differences between this and super keyword?
There are the following differences between this and super keyword.
o The super keyword always points to the parent class contexts whereas this keyword
always points to the current class context.
o The super keyword is primarily used for initializing the base class variables within the
derived class constructor whereas this keyword primarily used to differentiate between
local and instance variables when passed in the class constructor.
o The super and this must be the first statement inside constructor otherwise the compiler
will throw an error.

70) Can you use this() and super() both in a constructor?


No, because this() and super() must be the first statement in the class constructor.

71)What is object cloning?


The object cloning is used to create the exact copy of an object. The clone() method of the
Object class is used to clone an object. The [Link] interface must be implemented
by the class whose object clone we want to create. If we don't implement Cloneable interface,
clone() method generates CloneNotSupportedException.
1. protected Object clone() throws CloneNotSupportedException

72) What is method overloading?


Method overloading is the polymorphism technique which allows us to create multiple methods
with the same name but different signature. We can achieve method overloading in two ways.
o Changing the number of arguments
o Changing the return type
Method overloading increases the readability of the program. Method overloading is performed
to figure out the program quickly.

78) What is method overriding:


If a subclass provides a specific implementation of a method that is already provided by its
parent class, it is known as Method Overriding. It is used for runtime polymorphism and to
implement the interface methods.
Rules for Method overriding
o The method must have the same name as in the parent class.
o The method must have the same signature as in the parent class.
o Two classes must have an IS-A relationship between them.
More Details.

79) Can we override the static method?


No, you can't override the static method because they are the part of the class, not the object.

80) Why can we not override static method?


It is because the static method is the part of the class, and it is bound with class whereas instance
method is bound with the object, and static gets memory in class area, and instance gets memory
in a heap.

81) Can we override the overloaded method?


Yes.

82) Difference between method Overloading and Overriding.

Method Overloading Method Overriding

1) Method overloading Method overriding provides the specific


increases the readability of the implementation of the method that is already provided
program. by its superclass.

2) Method overloading occurs Method overriding occurs in two classes that have IS-A
within the class. relationship between them.

3) In this case, the parameters In this case, the parameters must be the same.
must be different.

83) Can we override the private methods?


No, we cannot override the private methods because the scope of private methods is limited to
the class and we cannot access them outside of the class.

88) What is covariant return type?


Now, since java5, it is possible to override any method by changing the return type if the return
type of the subclass overriding method is subclass type. It is known as covariant return type. The
covariant return type specifies that the return type may vary in the same direction as the subclass.

90) What is the final variable?


In Java, the final variable is used to restrict the user from updating it. If we initialize the final
variable, we can't change its value. In other words, we can say that the final variable once
assigned to a value, can never be changed after that. The final variable which is not assigned to
any value can only be assigned through the class constructor.

91) What is the final method?


If we change any method to a final method, we can't override it. More Details.

92) What is the final class?


If we make any class final, we can't inherit it into any of the subclasses.

95) Can you declare the main method as final?


Yes, We can declare the main method as public static final void main(String[] args){}.

98) Can we declare a constructor as final?


The constructor can never be declared as final because it is never inherited. Constructors are not
ordinary methods; therefore, there is no sense to declare constructors as final. However, if you
try to do so, The compiler will throw an error.

99) Can we declare an interface as final?


No, we cannot declare an interface as final because the interface must be implemented by some
class to provide its definition. Therefore, there is no sense to make an interface final. However, if
you try to do so, the compiler will show an error.

100) What is the difference between the final method and abstract method?
The main difference between the final method and abstract method is that the abstract method
cannot be final as we need to override them in the subclass to give its definition.

101) What is the difference between compile-time polymorphism and runtime polymorphism?
There are the following differences between compile-time polymorphism and runtime
polymorphism.

S compile-time polymorphism Runtime polymorphism


N

1 In compile-time In runtime polymorphism, call to an overridden


polymorphism, call to a method is resolved at runtime.
method is resolved at compile-
time.

2 It is also known as static It is also known as dynamic binding, late binding,


binding, early binding, or overriding, or dynamic method dispatch.
overloading.
3 Overloading is a way to Overriding is a way to achieve runtime
achieve compile-time polymorphism in which, we can redefine some
polymorphism in which, we particular method or variable in the derived class.
can define multiple methods or By using overriding, we can give some specific
constructors with different implementation to the base class properties in the
signatures. derived class.

4 It provides fast execution It provides slower execution as compare to


because the type of an object is compile-time because the type of an object is
determined at compile-time. determined at run-time.

5 Compile-time polymorphism Run-time polymorphism provides more


provides less flexibility flexibility because all the things are resolved at
because all the things are runtime.
resolved at compile-time.

102) What is Runtime Polymorphism?


Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden
method is resolved at runtime rather than at compile-time. In this process, an overridden method
is called through the reference variable of a superclass. The determination of the method to be
called is based on the object being referred to by the reference variable.

103) Can you achieve Runtime Polymorphism by data members?


No, because method overriding is used to achieve runtime polymorphism and data members
cannot be overridden. We can override the member functions but not the data members. Consider
the example given below.

104) What is the difference between static binding and dynamic binding?
In case of the static binding, the type of the object is determined at compile-time whereas, in the
dynamic binding, the type of the object is determined at runtime.

108) What is the difference between abstraction and encapsulation?


Abstraction hides the implementation details whereas encapsulation wraps code and data into a
single unit.

109) What is the abstract class?


A class that is declared as abstract is known as an abstract class. It needs to be extended and its
method implemented. It cannot be instantiated. It can have abstract methods, non-abstract
methods, constructors, and static methods. It can also have the final methods which will force the
subclass not to change the body of the method.

112) Can you use abstract and final both with a method?
No, because we need to override the abstract method to provide its implementation, whereas we
can't override the final method.

113) Is it possible to instantiate the abstract class?


No, the abstract class can never be instantiated even if it contains a constructor and all of its
methods are implemented.

115) Can you declare an interface method static?


No, because methods of an interface are abstract by default, and we can not use static and
abstract together.

116) Can the Interface be final?


No, because an interface needs to be implemented by the other class and if it is final, it can't be
implemented by any class.

117) What is a marker interface?


A Marker interface can be defined as the interface which has no data member and member
functions. For example, Serializable, Cloneable are marker interfaces. The marker interface can
be declared as follows.

121) How to make a read-only class in Java?


A class can be made read-only by making all of the fields private. The read-only class will have
only getter methods which return the private property of the class to the main method. We cannot
modify this property because there is no setter method available in the class.

122) How to make a write-only class in Java?


A class can be made write-only by making all of the fields private. The write-only class will have
only setter methods which set the value passed from the main method to the private fields. We
cannot read the properties of the class because there is no getter method in this class.

123) What are the advantages of Encapsulation in Java?


There are the following advantages of Encapsulation in Java?
o By providing only the setter or getter method, you can make the class read-only or write-
only. In other words, you can skip the getter or setter methods.
o It provides you the control over the data. Suppose you want to set the value of id which
should be greater than 100 only, you can write the logic inside the setter method. You can
write the logic not to store the negative numbers in the setter methods.
o It is a way to achieve data hiding in Java because other class will not be able to access the
data through the private data members.
o The encapsulate class is easy to test. So, it is better for unit testing.
o The standard IDE's are providing the facility to generate the getters and setters. So, it is
easy and fast to create an encapsulated class in Java.

124) What is the package?


A package is a group of similar type of classes, interfaces, and sub-packages. It provides access
protection and removes naming collision. The packages in Java can be categorized into two
forms, inbuilt package, and user-defined package. There are many built-in packages such as
Java, lang, awt, javax, swing, net, io, util, sql, etc. Consider the following example to create a
package in Java.

127) How can we access some class in another class in Java?


There are two ways to access a class in another class.
o By using the fully qualified name: To access a class in a different package, either we
must use the fully qualified name of that class, or we must import the package containing
that class.
o By using the relative path, We can use the path of the class that is related to the package
that contains our class. It can be the same or subpackage.

128) Do I need to import [Link] package any time? Why?


No. It is by default loaded internally by the JVM.

130) What is the static import?


By static import, we can access the static members of a class directly, and there is no to qualify it
with the class name.

141) What is the difference between throw and throws?

throw keyword throws keyword

1) The throw keyword is used to The throws keyword is used to declare an


throw an exception explicitly. exception.

2) The checked exceptions cannot The checked exception can be propagated with
be propagated with throw only. throws

3) The throw keyword is followed The throws keyword is followed by class.


by an instance.
4) The throw keyword is used The throws keyword is used with the method
within the method. signature.

5) You cannot throw multiple You can declare multiple exceptions, e.g., public
exceptions. void method()throws IOException, SQLException.
More details.

146) What is exception propagation?


An exception is first thrown from the top of the stack and if it is not caught, it drops down the
call stack to the previous method, If not caught there, the exception again drops down to the
previous method, and so on until they are caught or until they reach the very bottom of the call
stack. This procedure is called exception propagation. By default, checked exceptions are not
propagated.

154) Why java uses the concept of the string literal?


To make Java more memory efficient (because no new objects are created if it exists already in
the string constant pool).
More details.

155) How many objects will be created in the following code?


1. String s = new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).

158) What are the differences between String and StringBuffer?


The differences between the String and StringBuffer is given in the table below.

No. String StringBuffer

1 The String class is immutable. The StringBuffer class is


) mutable.

2 The String is slow and consumes more memory The StringBuffer is fast and
) when you concat too many strings because every consumes less memory when
time it creates a new instance. you cancat strings.

3 The String class overrides the equals() method of The StringBuffer class doesn't
) Object class. So you can compare the contents of override the equals() method
two strings by equals() method. of Object class.
159) What are the differences between StringBuffer and StringBuilder?
The differences between the StringBuffer and StringBuilder is given below.

No. StringBuffer StringBuilder

1) StringBuffer is synchronized, i.e., StringBuilder is non-synchronized,i.e.,


thread safe. It means two threads can't not thread safe. It means two threads can
call the methods of StringBuffer call the methods of StringBuilder
simultaneously. simultaneously.

2) StringBuffer is less efficient than StringBuilder is more efficient than


StringBuilder. StringBuffer.

161) What is the purpose of toString() method in Java?


The toString() method returns the string representation of an object. If you print any object, java
compiler internally invokes the toString() method on the object. So overriding the toString()
method, returns the desired output, it can be the state of an object, etc. depending upon your
implementation. By overriding the toString() method of the Object class, we can return the
values of the object, so we don't need to write much code.

162) Why CharArray() is preferred over String to store the password?


String stays in the string pool until the garbage is collected. If we store the password into a
string, it stays in the memory for a longer period, and anyone having the memory-dump can
extract the password as clear text. On the other hand, Using CharArray allows us to set it to
blank whenever we are done with the password. It avoids the security threat with the string by
enabling us to control the memory.

165) How the metacharacters are different from the ordinary characters?
Metacharacters have the special meaning to the regular expression engine. The metacharacters
are ^, $, ., *, +, etc. The regular expression engine does not consider them as the regular
characters. To enable the regular expression engine treating the metacharacters as ordinary
characters, we need to escape the metacharacters with the backslash.

168) What are the advantages of Java inner classes?


There are two types of advantages of Java inner classes.
o Nested classes represent a special type of relationship that is it can access all the
members (data members and methods) of the outer class including private.
o Nested classes are used to develop a more readable and maintainable code because it
logically groups classes and interfaces in one place only.
o Code Optimization: It requires less code to write.
169) What is a nested class?
The nested class can be defined as the class which is defined inside another class or interface.
We use the nested class to logically group classes and interfaces in one place so that it can be
more readable and maintainable. A nested class can access all the data members of the outer
class including private data members and methods. The syntax of the nested class is defined
below.

170) What are the disadvantages of using inner classes?


There are the following main disadvantages of using inner classes.
o Inner classes increase the total number of classes used by the developer and therefore
increases the workload of JVM since it has to perform some routine operations for those
extra classes which result in slower performance.
o IDEs provide less support to the inner classes as compare to the top level classes and
therefore it annoys the developers while working with inner classes.

171) What are the types of inner classes (non-static nested class) used in Java?
There are mainly three types of inner classes used in Java.

Type Description

Member Inner A class created within class and outside method.


Class

Anonymous A class created for implementing an interface or extending class. Its


Inner Class name is decided by the java compiler.

Local Inner A class created within the method.


Class

172) Is there any difference between nested classes and inner classes?
Yes, inner classes are non-static nested classes. In other words, we can say that inner classes are
the part of nested classes.

More details. 177) Can a class have an interface?


Yes, an interface can be defined within the class. It is called a nested interface.
More details.

178) Can an Interface have a class?


Yes, they are static implicitly.
More details.
179) What is Garbage Collection?
Garbage collection is a process of reclaiming the unused runtime objects. It is performed for
memory management. In other words, we can say that It is the process of removing unused
objects from the memory to free up space and make this space available for Java Virtual
Machine. Due to garbage collection java gives 0 as output to a variable whose value is not set,
i.e., the variable has been defined but not initialized. For this purpose, we were using free()
function in the C language and delete() in C++. In Java, it is performed automatically. So, java
provides better memory management.
More details.

180) What is gc()?


The gc() method is used to invoke the garbage collector for cleanup processing. This method is
found in System and Runtime classes. This function explicitly makes the Java Virtual Machine
free up the space occupied by the unused objects so that it can be utilized or reused. Consider the
following example for the better understanding of how the gc() method invoke the garbage
collector.

181) How is garbage collection controlled?


Garbage collection is managed by JVM. It is performed when there is not enough space in the
memory and memory is running low. We can externally call the [Link]() for the garbage
collection. However, it depends upon the JVM whether to perform it or not.

186) What is the difference between final, finally and finalize?

No. final finally finalize

1 Final is used to apply restrictions Finally is used to Finalize is used to


) on class, method, and variable. place important perform clean up
The final class can't be inherited, code, it will be processing just
final method can't be overridden, executed whether an before an object is
and final variable value can't be exception is handled garbage collected.
changed. or not.

2 Final is a keyword. Finally is a block. Finalize is a


) method.
193) What are the FileInputStream and FileOutputStream?
Java FileOutputStream is an output stream used for writing data to a file. If you have some
primitive values to write into a file, use FileOutputStream class. You can write byte-oriented as
well as character-oriented data through the FileOutputStream class. However, for character-
oriented data, it is preferred to use FileWriter than FileOutputStream. Consider the following
example of writing a byte into a file.
Java FileInputStream class obtains input bytes from a file. It is used for reading byte-oriented
data (streams of raw bytes) such as image data, audio, video, etc. You can also read character-
stream data. However, for reading streams of characters, it is recommended to use FileReader
class. Consider the following example for reading bytes from a file.

194) What is the purpose of using BufferedInputStream and BufferedOutputStream classes?


Java BufferedOutputStream class is used for buffering an output stream. It internally uses a
buffer to store data. It adds more efficiency than to write data directly into a stream. So, it makes
the performance fast. Whereas, Java BufferedInputStream class is used to read information from
the stream. It internally uses the buffer mechanism to make the performance fast.

207) Give a brief description of Java socket programming?


Java Socket programming is used for communication between the applications running on
different JRE. Java Socket programming can be connection-oriented or connectionless. Socket
and ServerSocket classes are used for connection-oriented socket programming and
DatagramSocket, and DatagramPacket classes are used for connectionless socket programming.
The client in socket programming must know two information:
o IP address of the server
o port number

208) What is Socket?


A socket is simply an endpoint for communications between the machines. It provides the
connection mechanism to connect the two computers using TCP. The Socket class can be used to
create a socket.

217) Can you access the private method from outside the class?
Yes, by changing the runtime behavior of a class if the class is not secured.
More details.

218)What are wrapper classes?


Wrapper classes are classes that allow primitive types to be accessed as objects. In other words,
we can say that wrapper classes are built-in java classes which allow the conversion of objects to
primitives and primitives to objects. The process of converting primitives to objects is called
autoboxing, and the process of converting objects to primitives is called unboxing. There are
eight wrapper classes present in [Link] package is given below.

Primitive Wrapper
Type class

Boolean Boolean

Char Character

Byte Byte

Short Short

Int Integer

Long Long

Float Float

Double Double

223) What is a native method?


A native method is a method that is implemented in a language other than Java. Natives methods
are sometimes also referred to as foreign methods.

227) What is a singleton class?


Singleton class is the class which can not be instantiated more than once. To make a class
singleton, we either make its constructor private or use the static getInstance method.

229) Which containers use a border layout as their default layout?


The Window, Frame and Dialog classes use a border layout as their default layout.

230) Which containers use a FlowLayout as their default layout?


The Panel and Applet classes use the FlowLayout as their default layout.

231) What are peerless components?


The lightweight component of Swing is called peerless components. Spring has its libraries, so it
does not use resources from the Operating System, and hence it has lightweight components.
232) is there is any difference between a Scrollbar and a ScrollPane?
The Scrollbar is a Component whereas the ScrollPane is a Container. A ScrollPane handles its
events and performs its scrolling.

233) What is a lightweight component?


Lightweight components are the one which does not go with the native call to obtain the
graphical units. They share their parent component graphical units to render them. For example,
Swing components, and JavaFX Components.

234) What is a heavyweight component?


The portable elements provided by the operating system are called heavyweight components.
AWT is limited to the graphical classes provided by the operating system and therefore, It
implements only the minimal subset of screen elements supported by all platforms. The
Operating system dependent UI discovery tools are called heavyweight components.

2) What is the thread?


A thread is a lightweight subprocess. It is a separate path of execution because each thread runs
in a different stack frame. A process may contain multiple threads. Threads share the process
resources, but still, they execute independently.

5) What is the purpose of wait() method in Java?


The wait() method is provided by the Object class in Java. This method is used for inter-thread
communication in Java. The [Link]() is used to pause the current thread, and wait
until another thread does not call the notify() or notifyAll() method. Its syntax is given below.
public final void wait()

6) Why must wait() method be called from the synchronized block?


We must call the wait method otherwise it will
throw [Link] exception. Moreover, we need wait() method
for inter-thread communication with notify() and notifyAll(). Therefore It must be present in the
synchronized block for the proper and correct communication.

14) What is the difference between wait() and sleep() method?

wait() sleep()

1) The wait() method is defined in Object The sleep() method is defined in Thread
class. class.

2) The wait() method releases the lock. The sleep() method doesn't release the
lock.
1. Why Selenium is used not Java?
 Selenium is a portable framework for testing web applications. Selenium provides a
playback tool for authoring functional tests without the need to learn a test scripting
language. The test can then run against most modern web browsers.

You might also like