Core java interview Questions You'll Most Likely Be Asked (Second Edition)


What is the default value for int, double, float, boolean


Download 1.52 Mb.
Pdf ko'rish
bet15/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   11   12   13   14   15   16   17   18   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

What is the default value for int, double, float, boolean,
long and char data types?
Answer:
The default values for the data types mentioned above are as
follows:
int = 0
double = 0.0d
float = 0.0f
boolean = false
long = 0L
char = ‘u0000’
In the following code snippet, explain which lines will
compile fine and which will cause a compilation error.
int [] [] add = new int [2] []; //Line 1
int [] subtract = new int [2]; //Line 2
int iValue = 2; //Line 3
add[1] = subtract; //Line 4
add[1] = iValue; //Line 5
Answer:
Line 1 compiles fine as it creates a two–dimensional array. Line 2
compiles fine as it creates a single dimensional array. Line 3


027.
028.
a.
b.
c.
d.
compiles fine as it declares and initializes an int variable.
Since a two– dimensional array is an array of arrays, Line 4 also
compiles fine. It assigns a single dimensional array to one of the
dimensions in the two–dimensional array. Line 5 causes a
compilation error because we cannot assign an int variable to an
int array variable.
Explain what happens when the following code is
compiled.
public class Car {
public Car (String carName) {}
}
Car myCar = new Car(“Benz”);
Car [] myCars = { myCar, new Car(“Ford”), new
Car(“BMW”)};
Answer:
The code compiles fine, there is no issue at all. The code first
creates a Car class. It creates a car object called myCar. Finally,
it creates a Car array called myCars which is initialized with
three values. The Car constructor is directly invoked during array
initialization.
You are given an assignment to create a game of Tic Tac
Toe using a multi–dimensional array. Which of the

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   11   12   13   14   15   16   17   18   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling