If the range of a number comes under the range -128 to 127 .This comes under which simple (or elemental) types of data:
long
int
short
byte
If the range of a number is 64 4.9e–324 to 1.8e+308
Then it belongs to which type of data type?
long
char
float
double
Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
ch1 and ch2
88 + Y
ch1 and ch2
88 + “ “ + Y
ch1 and ch2
88 “ “ Y
ch1 and ch2
88 Y
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
System.out.println("y is: " + y); // this always prints -1
y = 100;
System.out.println("y is now: " + y);
}
}
}
What is the output of this program?
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: 1
y is now: 100
y is: 1
y is now: 100
y is: 1
y is now: 100
y is: -1
y is now:-100
y is: -1
y is now: -100
y is: -1 y is now: -100
Demonstrate a one-dimensional array.
class Array {
THEVA
LANGUAGE
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}
What is the out put of the above program?
April has 28 days
April has 30 days
April has 31 days
None of the above
An improved version of the previous program.
class AutoArray {
public static void main(String args[]) {
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
System.out.println("April has " + month_days[3] + " days.");
}
}
What is the out put of the above program?
April has 28 days
April has 30 days
April has 31 days
None of the above
Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j 0
1 2
3 4 5
6 7 8 9
01 2
3 4 5
6 7 8 9
0
1 23 4 5
6 7 8 9
None of the above
Left shifting as a quick way to multiply by 2.
class MultByTwo {
public static void main(String args[]) {
int i;
int num = 0xFFFFFFE;
for(i=0; i<4; i++) {
num = num << 1;
System.out.println(num);
}
}
}
The program generates the following output:
536870908
1073741816
2147483632
-32
536870908
1073741816
2147483632
32
536870908
1073741816
2147483632
-31
536870908
1073741816
2147483632
-30
Masking sign extension.
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}
what is the out put of the above program ?
b = 0xf1
b = x0f1
b = f0x1
b = 10xf
Demonstrate the boolean logical operators.
class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}
}
What will be the output of the above program?
a = false a&b = false
b = false a^b = true
a|b = true a&b|a&!b = true
!a = false
a = true a^b = true
b = false a&b|a&!b = true
a|b = true !a = false
a&b = false
Demonstrate ?.
class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
}
}
what is the output generated by this program?
Absolute value of 1 is -10
Absolute value of -1 is 10
Absolute value of 1 is 10
Absolute value of -1 is 10
Absolute value of 10 is 10
Absolute value of -10 is 10
Absolute value of 10 is -10
Absolute value of 10 is -10
Demonstrate if-else-if statements.
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}
What is the out put of the above program ?
April is in the Winter
April is in the Spring.
April is in the Summer.
April is in the Autumn.
// A simple example of the switch.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
What is the out put of the above program?
i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.
i is zero.
i is. two.
i is one
i is three.
i is greater than 3.
i is greater than 3.
Demonstrate the while loop.
class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
System.out.println("tick " + n);
n--;
}
}
}
When you run this program, it will “tick” ten times:
tick 4
tick 5
tick 6
tick 7
tick 8
tick 9
tick 10
tick 11
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
Using break to exit a loop.
class BreakLoop {
public static void main(String args[]) {
for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
This program generates the following output:
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8NGUAGE
i: 9
i: 10
Loop complete.
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8LANGUAGE
i: 9 Loop complete.
This program declares two Box objects.
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
What is new in the above program?
New wil create new object
New will create new instance of an class i.e object
New will create a object of a class
None of the above
// This program uses a parameterized method.
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
// sets dimensions of box
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
class BoxDemo5 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// initialize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
As you can see, the setDim( ) method is used to set the dimensions of each box. For example, when mybox1.setDim(10, 20, 15); is executed, 10 is copied into parameter w, 20 is copied into h, and 15 is copied into d. Inside setDim( ) the values of w, h, and d are then assigned to width, height, and depth, respectively.
What is setDim() in the above program ?
the setDim( ) method is used to set the dimensions of each box
the setDim( ) function is used to set the dimensions of each box
the setDim( ) procedure is used to set the dimensions of each box
Automatic type conversions apply to overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
void test(double a) {
System.out.println("Inside test(double) a: " + a);
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test();
ob.test(10, 20);
ob.test(i); // this will invoke test(double)
ob.test(123.2); // this will invoke test(double)
}
}
This program generates the following output:
No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2
No parameters
a and b: 10 20
Inside test(double) a: 78
Inside test(double) a: 123.2
No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
In the above constructor Box() it requires how many parameters ?
3
2
1
No parameter
// Objects may be passed to methods.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// return true if o is equal to the invoking object
boolean equals(Test o) {
if(o.a == a && o.b == b) return true;
else return false;
}
}
class PassOb {
public static void main(String args[]) {
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equals(ob2));
System.out.println("ob1 == ob3: " + ob1.equals(ob3));
}
}
What is the out put of the above program ?
ob1 == ob2: false
ob1 == ob3: true
THE
ob1 == ob2: true
ob1 == ob3: false
THE
ob1 == ob2: true
ob1 == ob3: false
THE
None of the above