System.out.println("This is a simple Java program.");
Which of the following is /are true regarding the above statement?LANGUAGE
This line outputs the string “This is a simple Java program.” followed by a new line on the screen.
Output is actually accomplished by the built-in println( ) method
The line begins with System.out.While too complicated to explain in detail at this time, briefly, System is a predefined class that provides access to the system, and out is the output stream that is connected
to the console.
All of the above
The Simple Types
Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float, double, and boolean. These can be put in four groups:
?¡ Integers
?¡ Floating-point numbers
?¡ Characters.
?¡ Boolean
You can use these types as-is, or to construct arrays or your own class types. Thus, they form the basis for all other types of data that you can create.
Which group includes a datatype which is a special type for representing true/false values.
Integers
Floating-point numbers
Characters.
Boolean
the area of a circle.
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
THE
JAVA
System.out.println("Area of circle is " + a);
}
}
what is the out put of the program ?
Area of circle is 366.436224
Area of circle is 3666.436224
Area of circle is 36.43624
Area of circle is 364.36224
Demonstrate dynamic initialization.
class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}
Here, three local variables—a, b,and c—are declared. The first two, a and b, are initialized by constants. However, c is initialized------------------------------------- to the length of the hypotenuse (using the Pythagorean theorem). The program uses another of Java’s
built-in methods, sqrt( ), which is a member of the Math class, to compute the square root of its argument. The key point here is that the initialization expression may use any element valid at the time of the initialization, including calls to methods, other
variables, or literals.
statically
dynamically
instantly
None of the above
class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}
What is the type for the final result of the expression?
int
float
double
None of the above
Demonstrate a three-dimensional array.
class threeDMatrix {
public static void main(String args[]) {
int threeD[][][] = new int[3][4][5];
int i, j, k;
for(i=0; i<3; i++)
for(j=0; j<4; j++)
for(k=0; k<5; k++)
threeD[i][j][k] = i * j * k;
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
for(k=0; k<5; k++)
System.out.print(threeD[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}
What is the out put of the above program ?
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
4 4 4 4 4
4 4 4 4 4
4 4 4 4 4
4 4 4 4 4
The following program demonstrates the bitwise logical operators:
// Demonstrate the bitwise logical operators.
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}
What is the output of the above program?
a = 0111 a&b = 0010
b = 0110 a^b = 0101
a|b = 0111 ~a&b|a&~b = 0101
~a = 1100
a = 0011
b = 0110
a|b = 0111
a&b = 0010
a^b = 0101
~a&b|a&~b = 0101
~a = 1100
The target of a loop can be empty.
class NoBody {
public static void main(String args[]) {
int i, j;
i = 100;
j = 200;
// find midpoint between i and j
while(++i < --j) ; // no body in this loop
System.out.println("Midpoint is " + i);
}
}
What will be the output of the above program ?
Midpoint is 150
Midpoint is 15
Midpoint is 50
Midpoint is 250
Using the comma.
class Comma {
public static void main(String args[]) {
int a, b;
for(a=1, b=4; a a = 1
b = 2
a = 3
b = 4
a = 1
b = 4
a = 2
b = 3
a = 4
b = 3
a = 2
b = 1
a = 5
b = 4
a = 3
b = 2
Loops may be nested.
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
System.out.print (".");
System.out.println();
}
}
}
what is the out put of the above program ?
.........
........
.......
......
.....
....
...
..
.........
........
.......
......
.....
....
...
..
.
// Using break as a civilized form of goto.
class Break {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
Running this program generates the following output:
Before the break.
This is after second block.
LANGUAGE
Before the break.
This is after second block
This won't execute
This is after second block
None of the above
A// This program includes a method inside the box class.
class Box {
double width;
double height;
double depth;
// display volume of a box
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume();
// display volume of second box
mybox2.volume();
}
}
What is the out put of the above program?
Volume is 300.0
Volume is 162.0
Volume is 3000.0
Volume is 162.0
Volume is 3000.0
Volume is 16.0
/*Here, Box uses a constructor to initialize the
Dimensions of a box.
*/
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// 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);
}
}
What is the output of the above program?
Constructing Box
Constructing Box
Volume is 1000.0
Volume is 1000.0
Constructing Box
Volume is 1000.0
Volume is 1000.0
Constructing Box
// A redundant use of this.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;
}
Which statement is true about this in the above program?
this will always refer to the invoking object.
this can be used inside any method to refer to the current object.
this is always a reference to the object on which the method was invoked.
All of the above
Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// 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
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}|
What is the out put of the above program ?
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
No parameters
a: 1
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
// Here, Box allows one object to initialize another.
class Box {
double width;
double height;
double depth;
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class OverloadCons2 {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
Box myclone = new Box(mybox1);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of cube is " + vol);
// get volume of clone
vol = myclone.volume();
System.out.println("Volume of clone is " + vol);
}
}
In the above program which is the construct clone of an object?
Box(double len)
Box(Box ob)
// Demonstrate static variables, methods, and blocks.
class UseStatic {E
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
what is the out put of the above program ?
Static block initialized.
x = 42
a = 32
b = 12
Static block initialized.
x = 48
a = 3
b = 12
Static block initialized.
x = 42
a = 3
b = 12
None of the above
// Define an inner class within a for loop.
class Outer {
int outer_x = 100;
void test() {
for(int i=0; i<10; i++) {
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
Inner inner = new Inner();
inner.display();
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}
what is the output of the above program ?
display: outer_x = 100 display: outer_x = 100
display: outer_x = 100 display: outer_x = 100
display: outer_x = 100 display: outer_x = 100
display: outer_x = 100 display: outer_x = 100
display: outer_x = 100 display: outer_x = 100
display: outer_x = 101
display: outer_x = 101
display: outer_x = 101
display: outer_x = 101
display: outer_x = 101
display: outer_x = 101
display: outer_x = 101
display: outer_x = 101
display: outer_x = 101
display: outer_x = 101
JAVA and JavaScript are same
FALSE
TRUE
Which web technology with its appropriate task is used to Identify text as a second-level heading
CSS
HTML and XHTML
Ruby on Rails
JavaScript
XML