Blog

CORE JAVA INTERVIEW QUESTIONS

java interview questions

Core Java Interview Questions : Core Java, is one of the most important subject in programming stream at the time of interview.

These questions will help you to easily crack core java interview questions.

Today in this blog we will share CORE JAVA INTERVIEW QUESTIONS and their answers.

What are the primitive data types in Java ?

Primitive types are the most basic data types available within the Java language. These types serve as the building blocks of data manipulation in Java.

Such types serve only one purpose — containing pure, simple values of a kind.

Because these data types are defined into the Java type system by default, they come with a number of operations predefined.

You can not define a new operation for such primitive types.

There are 8: boolean, byte, char, short, int, long, float and double.

What is Autoboxing and Unboxing?

Autoboxing refers to the automatic conversion of primitive types to its corresponding object wrapper classes.

For example, converting an int to an Integer, a double to a Double etc.

This automatic conversion is accomplished by the compiler.

Character ch = ‘c’;// char autoboxed to its wrapper class.

Float fl = 2.5f;  //float converted to Float object.

Is Java primitive data type stored on stack or heap?

Primitive types declared as local variable will be stored on the stack memory .

 Primitive types that are defined as part of an object instance know as instance variable are stored on the heap.

Primitive types that are defined as class variable know as static variable are stored on the method area.

What is the default value of local variables in Java?

 no default value  is for local variables.

Can you compare a boolean with an int variable in Java?

No.

Difference between double and float variables in Java.

In java, float takes 4 bytes in memory while Double takes 8 bytes in memory.

float is single precision floating point decimal number while Double is double precision decimal number.

What is the default value of char data type in Java?

The default value of a char primitive type is ‘\u0000′(null character) as stated in the Java Language Specification.

The shortcut for ‘u0000’ is ‘\0’

What are the Wrapper classes available for primitive types ?

boolean  – java.lang.Boolean

byte – java.lang.Byte

char – java.lang.Character

double – java.lang.Double

float – java.lang.Float

int – java.lang.Integer

long – java.lang.Long

short – java.lang.Short

void – java.lang.Void

core java interview questions
BEST INSTITUTE FOR CORE JAVA TRAINING IN INDORE

.

Let us discuss some more questions. Hope you guys are liking our blog.

Difference between long.Class and Long.TYPE ?

 They both represent the long primitive type. They are exactly the same.

 What are wrapper classes ?

 They are wrappers to primitive data types. They allow us to access primitives as objects.

 What is casting?

 There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference

 What are the default or implicitly assigned values for data types in java ?

 boolean —> false

byte —-> 0

short —-> 0

int —–> 0

long ——> 0l

char —–> /u0000

float ——> 0.0f

double —-> 0.0d

any object reference —-> null

is java  strongly typed language?

Yes, java is strongly typed language. A strongly-typed language is one in which variables are bound to specific data types, and will result in type errors if types do not match up as expected in the expression .

In java every variable and expression have a type.

e. g.     int  I = 3.5;

in above expression data type of I variable is int type and data type of literal 3.5 is double type. So we will get compilation error.

Difference between weakly and strongly typed language.

Weakly typed language makes conversions between unrelated types implicitly,(for example in C language we can declare int i = 3.5,we will not get any compilation error, C compiler will assign integral part of 3.5 double type to i, i.e. I int variable will be intialized by 3 ) .

CORE JAVA INTERVIEW QUESTIONS

While a strongly typed language typically disallows implicit conversions between unrelated types (for example in java language if we  declare int i = 3.5,we will not get  compilation error).

Furthermore a strongly typed language requires an explicit conversion (by using the cast operator) between related types, when there is possibility of data loss, while a weakly typed one would carry out the conversion regardless.

What are Primitive Literals?

 Primitive Literals are the code representation of values of primitive data types. For example ‘a’ is a char literal, 100 is an int literal, ‘false’ is a boolean literal and 2345.456 is a double literal.

CORE JAVA INTERVIEW QUESTION
JAVA INTERVIEW QUESTIONS ASKED IN CORPORATE INDUSTRIES

What is Primitive Casting in Java programming language?

Java provides the facility of mixing different types of variables and constants in an expression. In these types of operations data type of one operand is converted into data type of another operand. This is known as type conversion. The different types of type conversion are

Implicit type conversions or widening or automatic pramotion are done by the compiler while the explicit type conversions or narrowing are user definecd conversions.

CORE JAVA INTERVIEW QUESTIONS

Implicit type  conversions are done by the Java compiler according to some predefined rules of java language.

byte->short->int->long->float->double

              char

e.g.           byte byteVar =20;

                 short   shortVar = byteVar;  // (widening or implicit type casting)

For example     float z;

int x = 20, y = 3; z = x/y;

The value of z will be 6.0 instead of 6.66.

If we want correct value of z then we should use cast operator

z =(float) x/y;   // explicit type casting

The value of z will be  6.66.

How long do primitive variables exist in memory? Define the various scopes of primitive variables?

or

After a primitive variable is declared and initialized; how long it lives in memory

Scope of a variable is determined based on where it is declared within a java class.

 various scopes of a variable in a java program based on where they are declared.

1. Class variable (Static fields) – Class variables are variables declared within the class body, outside of any methods or blocks, and declared with ‘static’ keyword.

Class variables have the longest scope. They are created when the class is loaded, and remain in memory as long as the class remains loaded in JVM.

2. Instance variables (Non-static fields) – Instance variable are variables declared within the class body, outside of any method or block, and declared without ‘static’ keyword.

Check Out CORPORATE TRAINING ON CORE JAVA

Instance variables have the second highest scope. Instance variables are created when a new class instance is created, and live until the instance is removed from memory.

3. Local Variables – Local variables are variables declared within a method body or within a constructor . They live only as long as the method (or constructor) in which it is declared remains on the stack.

4. Block variables – Block variables are variables declared within a block such as an init block or within a for loop. They live only during the execution of the block and are the shortest living variables.

How does Java programming language pass primitive variables to methods – by value or by reference?

  In Java, primitive variables are passed to methods by value. More specifically, a copy of the primitive value is passed to the method. If the passed value changes in the method, it does not change the original value.

In how many ways an integer value can be represented?

There are three ways to represent integer value –

  • Decimal – it is a base 10 representation. This is default format.
  • Octal – it is a base 8 representation. We need to use prefix 0 with values.
  • HexaDecimal – it is a base 16 representation. We need to use case insensitive  prefix 0x.

How to convert a primitive type to wrapper class?

There are two ways in which we can convert primitive data type to a wrapper objects.

  • Constructor- Every wrapper class constructor takes the primitive value and returns corresponding wrapper object.
  • Utility Class- Every type has a utility classes which exposes static methods valueOf()  and takes the primitive value like Float.valueOf(“2.3f”)

How to convert a wrapper object to primitive data type?

Primitive values can be obtained from wrapper classes by calling xxxValue() methods of wrapper class where xxx is primitive data type like

Integer it = new Integer(23);

int primitive= it.intValue();

is String is primitive data type in Java ?

No. String is pre-defined class in java.

What do you mean by Non-Primitive Datatypes or user-defined data types or custom data types?

Non-Primitive (user defined or custom)data types refer to objects and hence they are called reference types. Examples of non-primitive types include Strings, Arrays, Classes, Interface, etc.

Difference between primitive and non-primitive data types

  • Primitive types are predefined in Java. Non-primitive types are created by the programmer and is not defined by Java.
  • Non Primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  • A primitive type always has a value, whereas non-primitive types can be null.
  • A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.

How we can create constants in Java?

By using final modifier , we can declare a variable as constant in java.

e.g.

               final int X=20;

               final  boolean Z =false;

 What are naming conventions to declare a variable in Java?

All java variables should start with lowercase letter and inner words start with uppercase letter. 

Ex:-  pageContent  bodyContent

What are naming conventions to declare a constants in Java?

All java constant variables should be in uppercase letter.

 e.g.                                     final float PI=3.14f;

We hope you like our blog. Check out more blogs on programming languages in our blog section and also do read java interview question and important java interview questions.

We will be back with some more technical blogs. Check out our industry ready corporate training programs on core java to get placement in best IT companies.

Leave a Reply