CORE JAVA INTERVIEW QUESTIONS AND ANSWERS – PART 2

In this post, Java Interview Questions and Answers are discussed, which are helpful for Java beginners and Experienced persons. When you for for Selenium Test Automation Interview, you can expect similar kind of questions.

21. What is ASCII Code?

ASCII stands for American Standard code for Information Interchange. ASCII character range is 0 to 255. We can’t add more characters to the ASCII Character set. ASCII character set supports only English. That

is the reason, if we see C language we can write c language only in English we can’t write in other languages because it uses ASCII code.

22. What is Unicode ?

Unicode is a character set developed by Unicode Consortium. To support all languages in the world Java supports Unicode values. Unicode characters were represented by 16 bits and its character range is 0- 65,535.

Java uses ASCII code for all input elements except for Strings,identifiers, and comments. If we want to use telugu we can use telugu characters for identifiers.We can enter comments in telugu.

23. Difference between Character Constant and String Constant in java ?

Character constant is enclosed in single quotes. String constants are enclosed in double quotes. Character constants are single digit or character. String Constants are collection of characters.

Ex :’2’, ‘A’

Ex : “Hello World”

24. What are constants and how to create constants in java?

Constants are fixed values whose values cannot be changed during the execution of program. We create constants in java using final keyword.

Ex : final int number =10;

final String str=”java-interview –questions”

25. Difference between ‘>>’ and ‘>>>’ operators in java?

>> is a right shift operator shifts all of the bits in a value to the right to a specified number of times. int a =15;

a= a >> 3;

The above line of code moves 15 three characters right.

>> is an unsigned shift operator used to shift right. The places which were vacated by shift are filled with zeroes.

26. Explain Java Coding Standards for classes or Java coding conventions for classes?

Sun has created Java Coding standards or Java Coding Conventions . It is recommended highly to follow java coding standards.

Classnames should start with uppercase letter. Classnames names should be nouns. If Class name is of multiple words then the first letter of inner word must be capital letter.

Ex : Employee, EmployeeDetails, ArrayList, TreeSet, HashSet

27. Explain Java Coding standards for interfaces?

  1. Interface should start with uppercase letters
  2. Interfaces names should be adjectives Example : Runnable, Serializable, Marker, Cloneable

28. Explain Java Coding standards for Methods?

  1. Method names should start with small letters.
  2. Method names are usually verbs
  3. If method contains multiple words, every inner word should start with uppercase letter. Ex : toString()
  4. Method name must be combination of verb and noun Ex : getCarName(),getCarNumber()29.

29. Explain Java Coding Standards for variables ?

  1. Variable names should start with small letters.
  2. Variable names should be nouns
  3. Short meaningful names are recommended.
  4. If there are multiple words every innerword should start with Uppecase character. Ex : string,value,empName,empSalary

30. Explain Java Coding Standards for Constants?

Constants in java are created using static and final keywords.

  1. Constants contains only uppercase letters.
  2. If constant name is combination of two words it should be separated by underscore.
  3. Constant names are usually nouns.

Ex:MAX_VALUE, MIN_VALUE, MAX_PRIORITY, MIN_PRIORITY

31. Difference between overriding and overloading in java?

Overriding Overloading
In overriding method names must be same In overloading method names must be same
Argument List must be same Argument list must be different atleast order of arguments.
Return type can be same or we can return covariant type. From 1.5 covariant types are allowed Return type can be different in overloading.
We cant increase the level of checked exceptions. No restrictions for unchecked exceptions In overloading different exceptions can be thrown.
A method can only be overridden in subclass A method can be overloaded in same class or subclass
Private,static and final variables cannot be overridden. Private , static and final variables can be overloaded.
In overriding which method is called is decided at runtime based on the type of object referenced at run time In overloading which method to call is decided at compile time based on reference type.
Overriding is also known as Runtime polymorphism, dynamic polymorphism or late binding Overloading is also known as Compile time polymorphism, static polymorphism or early binding.

32. What is ‘IS-A ‘ relationship in java?

‘is a’ relationship is also known as inheritance. We can implement ‘is a’ relationship or inheritance in java using extends keyword. The advantage or inheritance or is a relationship is reusability of code instead of duplicating the code.

Ex : Motor cycle is a vehicle

Car is a vehicle Both car and motorcycle extends vehicle.

33. What is ‘HAS A’’ relationship in java?

‘Has a ‘ relationship is also known as “composition or Aggregation”. As in inheritance we have ‘extends’ keyword we don’t have any keyword to implement ‘Has a’ relationship in java. The main advantage of ‘Has-A‘ relationship in java code reusability.

34. Difference between ‘IS-A’ and ‘HAS-A’ relationship in java?

IS-A relationship HAS- A RELATIONSHIP
Is a relationship also known as inheritance Has a relationship also known as composition or aggregation.
For IS-A relationship we uses extends keyword For Has a relationship we use new keyword
Ex : Car is a vehicle. Ex : Car has an engine. We cannot say Car is an engine
The main advantage of inheritance is reusability of code The main advantage of has a relationship is reusability of code.

35. Explain about instanceof operator in java?

Instanceof operator is used to test the object is of which type. Syntax : <reference expression> instanceof <destination type>

Instanceof returns true if reference expression is subtype of destination type. Instanceof returns false if reference expression is null.

Example : public classInstanceOfExample {public static voidmain(String[] args) {Integer a =

newInteger(5);if (a instanceof java.lang.Integer) { System.out.println(true);

} else { System.out.println(false);

}

}

}

Since a is integer object it returns true.

There will be a compile time check whether reference expression is subtype of destination type. If it is not a subtype then compile time error will be shown as Incompatible types

36. What does null mean in java?

When a reference variable doesn’t point to any value it is assigned null. Example : Employee employee;

In the above example employee object is not instantiate so it is pointed no where

37. Can we have multiple classes in single file ?

Yes we can have multiple classes in single file but it people rarely do that and not recommended. We can have multiple classes in File but only one class can be made public. If we try to make two classes in File public we get following compilation error.

“The public type must be defined in its own file”.

38. What all access modifiers are allowed for top class ?

For top level class only two access modifiers are allowed. public and default. If a class is declared as public it is visible everywhere.

If a class is declared default it is visible only in same package.

If we try to give private and protected as access modifier to class we get the below compilation error. Illegal Modifier for the class only public,abstract and final are permitted.

39. What are packages in java?

Package is a mechanism to group related classes ,interfaces and enums in to a single module. Package can be declared using the following statement :

Syntax : package <package-name>

Coding Convention : package name should be declared in small letters. package statement defines the namespace.

The main use of package is

  1. To resolve naming conflicts
  2. For visibility control : We can define classes and interfaces that are not accessible outside the class.

40. Can we have more than one package statement in source file ?

We can’t have more than one package statement in source file. In any java program there can be atmost only 1 package statement. We will get compilation error if we have more than one package statement in source file.

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories