As you would know, Java is one of the most popular object-oriented programming languages and has been in use since its release in 1995. What is particularly interesting is that test automation can be written in Java in certain automated testing tools/ frameworks. Here are some basic questions and answers related to the Java programming language. These questions and answers are excerpted from the notes that I created. View the videos, Java Interview Questions and Answers - 1
and Java Interview Questions and Answers - 2. Or read on.
1. Why is the Java bytecode a bit slower than native code?
The Java bytecode is platform-independent which can run unchanged on different platforms. It may not be optimized to run on a single platform like native code. However in future, advances in compiler and JVM may bring the performance of Java bytecode closer to native code.
The Java bytecode is platform-independent which can run unchanged on different platforms. It may not be optimized to run on a single platform like native code. However in future, advances in compiler and JVM may bring the performance of Java bytecode closer to native code.
2. What are the 3 types of comments in Java language?
The first type is the multi-line comment. Any text between /* and */ is ignored by the compiler. The second type is the single-line comment. Any text following // to the end of the line is ignored by the compiler. The last type is the documentation comment. Text between /** and */ is ignored by the compiler but read by the javadoc tool to generate documentation.
The first type is the multi-line comment. Any text between /* and */ is ignored by the compiler. The second type is the single-line comment. Any text following // to the end of the line is ignored by the compiler. The last type is the documentation comment. Text between /** and */ is ignored by the compiler but read by the javadoc tool to generate documentation.
3. Which statement could be used in place of multiple if statements in Java?
It is the switch statement. This statement can have a number of expected values for an expression. Each value can have its own set of statements (the last statement being the break statement) to execute. If the expression is not equal to any of the given values, the control goes to the default and its set of statements is executed.
It is the switch statement. This statement can have a number of expected values for an expression. Each value can have its own set of statements (the last statement being the break statement) to execute. If the expression is not equal to any of the given values, the control goes to the default and its set of statements is executed.
4. What is the difference between private and public access modifiers?
A method, a constructor (i.e. a method with the same name as the class) or a variable may be declared as private or public. Private means that the declared item is visible (can be accessed within) the class only. On the other hand, public means that the declared item is visible to all Java classes.
A method, a constructor (i.e. a method with the same name as the class) or a variable may be declared as private or public. Private means that the declared item is visible (can be accessed within) the class only. On the other hand, public means that the declared item is visible to all Java classes.
5. What are the types of loops?
One loop is the while loop. At the beginning of the loop, it has a condition that is then followed by the body of the loop. It checks the condition and if the condition is true, the body of the loop is executed and it checks the condition again. If the condition is false, the loop execution is terminated. Another loop is the for loop. It has three parts (initialization, condition and increment) followed by the body of the loop. Like the while loop, the for loop executes the body of the loop if the condition is true and stops executing if the condition is false. The difference from the while loop is that the initialization and increment expressions are given within the for statement. The other loop is the do while loop. It is like the while loop but has the body of the loop followed by the condition. Since it checks the condition after executing the body of the loop, it executes at least once.
One loop is the while loop. At the beginning of the loop, it has a condition that is then followed by the body of the loop. It checks the condition and if the condition is true, the body of the loop is executed and it checks the condition again. If the condition is false, the loop execution is terminated. Another loop is the for loop. It has three parts (initialization, condition and increment) followed by the body of the loop. Like the while loop, the for loop executes the body of the loop if the condition is true and stops executing if the condition is false. The difference from the while loop is that the initialization and increment expressions are given within the for statement. The other loop is the do while loop. It is like the while loop but has the body of the loop followed by the condition. Since it checks the condition after executing the body of the loop, it executes at least once.
6. What is the enhanced for loop?
This feature is a simplified way of looping through the elements of an array. It requires only two parts in the for statement. The first part is the variable and the second part is the name of the array. In the example below, x is the variable of the same datatype as the array called myArray. The enhanced for loop automatically loops through each element of the array.
String myArray[] = {"Aa","BBB","CcC","DDDD","EeEE"};
for (String x: myArray)
System.out.println(x);
This feature is a simplified way of looping through the elements of an array. It requires only two parts in the for statement. The first part is the variable and the second part is the name of the array. In the example below, x is the variable of the same datatype as the array called myArray. The enhanced for loop automatically loops through each element of the array.
String myArray[] = {"Aa","BBB","CcC","DDDD","EeEE"};
for (String x: myArray)
System.out.println(x);
7. What is a class constructor?
This is a method with the same name as the class. This method is called when a new object of the class is created. The constructor contains initialization code to set private variables, to call private methods and so on. There can be multiple constructors in a class (this is called overloading the constructors). Each of these constructors has different parameters like different number of parameters or maybe parameters of different data types.
This is a method with the same name as the class. This method is called when a new object of the class is created. The constructor contains initialization code to set private variables, to call private methods and so on. There can be multiple constructors in a class (this is called overloading the constructors). Each of these constructors has different parameters like different number of parameters or maybe parameters of different data types.
8. What is the difference between final and static modifiers?
The final modifier requires that the class field/ variable can be assigned a value only once. Java throws a compile time error if another statement to assign value to this variable is written. On the other hand, the static modifier shares the class field/ variable across all instances of the class. The static variable may be assigned a particular value in one object of the class and it retains the same value in another object of the class. A static variable can be used even if there is no existing object of the class.
The final modifier requires that the class field/ variable can be assigned a value only once. Java throws a compile time error if another statement to assign value to this variable is written. On the other hand, the static modifier shares the class field/ variable across all instances of the class. The static variable may be assigned a particular value in one object of the class and it retains the same value in another object of the class. A static variable can be used even if there is no existing object of the class.
9. What is inheritance?
A class may inherit another class by using the extends keyword. The former is then called the sub-class and the latter the super-class. All public members (fields and methods) of the super-class are available to the sub-class without writing any code. In addition, an object variable of the super-class type may be assigned any object reference of the sub-class type.
A class may inherit another class by using the extends keyword. The former is then called the sub-class and the latter the super-class. All public members (fields and methods) of the super-class are available to the sub-class without writing any code. In addition, an object variable of the super-class type may be assigned any object reference of the sub-class type.
10. What is an event handler?
The user can perform an action on a GUI element such a button or a textbox. Such an action can be moving focus in or out of the element or pressing Enter. The action is called an event. An event handler is the code associated with the GUI element and it executes when an event occurs.
The user can perform an action on a GUI element such a button or a textbox. Such an action can be moving focus in or out of the element or pressing Enter. The action is called an event. An event handler is the code associated with the GUI element and it executes when an event occurs.
In order to go to Java Questions and Answers Part 2, click here.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.