/
The code compiles and prints “A
The code compiles and prints “A
The code compiles and prints “B
The code compiles and prints “AB
Public class test (
Public static void stringReplace (String text)
(
Text = text.replace (‘j’ , ‘i’);
)
public static void bufferReplace (StringBuffer text)
(
text = text.append (“C”)
)
public static void main (String args[]}
(
String textString = new String (“java”);
StringBuffer text BufferString = new StringBuffer (“java”);
stringReplace (textString);
BufferReplace (textBuffer);
System.out.printIn (textString + textBuffer);
}
)
What is the output?
JavaJavaC
JavaCJava
Java
JavaC
public class test {
public static void add3 (Integer i)
{
int val = i.intValue ( );
val += 3;
i = new Integer (val);
}
public static void main (String args [ ] )
{
Integer i = new Integer (0);
add3 (i);
system.out.printIn (i.intValue ( ) );
}
)
What is the result?
Compilation will fail.
The program prints “0”.
The program prints “3”.
Compilation will succeed but an exception will be thrown at line 3.
Which two statements are reserved words in Java?
A. run
B. import
C. default
D. implement
B,C
A,B
A,D
B,D
Which will declare a method that forces a subclass to implement it?
Public double methoda();
Static void methoda (double d1) {}
Public native double methoda();
Abstract public void methoda();
Given the ActionEvent, which method allows you to identify the affected component?
getClass
getTarget
getSource
getComponent
Which can be used to encode charS for output?
java.io.OutputStream
java.io.OutputStreamWriter
java.io.EncodeOutputStream
java.io.EncodeWriter
The file “file.txt” exists on the file system and contains ASCII text.
Given:
38. try {
39. File f = new File(“file.txt”);
40. OutputStream out = new FileOutputStream(f, true);
41. }
42. catch (IOException) {}
What is the result?
The code does not compile.
The code runs and no change is made to the file.
The code runs and sets the length of the file to 0.
An exception is thrown because the file is not closed.
Which constructs a DataOutputStream?
new dataOutputStream(new writer(“out.txt”));
new dataOutputStream(new FileWriter(“out.txt”));
new dataOutputStream(new OutputStream(“out.txt”));
new dataOutputStream(new FileOutputStream(“out.txt”));
/*
Demonstrate the if.
Call this file "IfSample.java".
*/
class IfSample {
public static void main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x < y) System.out.println("x is less than y");
x = x * 2;
if(x == y) System.out.println("x now equal to y");
x = x * 2;
if(x > y) System.out.println("x now greater than y");
// this won't display anything
if(x == y) System.out.println("you won't see this");
}
}
When you run this program, what will be the output?JAVA
LANGUA
x is less than y
x now equal to y
x now greater than y
y is less than x
x now equal to y
x now greater than y
x is less than y
y now equal to x
y now greater than x
Y is less than x
x now equal to y
y now greater than x
public static void main(String args[]) {
int x, y;
y = 20;
// the target of this loop is a block
for(x = 0; x<10; x++) {
System.out.println("This is x: " + x);
System.out.println("This is y: " + y);
y = y - 2;
}
}
}
The output generated by this program is shown here:
This is x: 0
This is y: 20
This is x: 1
This is y: 18
This is x: 2
This is y: 16
This is x: 3
This is y: 14
This is x: 4
This is y: 12
This is x: 5
This is y: 10
This is x: 6
This is y: 8
This is x: 7
This is y: 6
This is x: 8
This is y: 4
This is x: 9
This is y: 2
Which statement is true regarding the above program?
each time the loop iterates, the three statements inside the block will be executed
two tims the loop iterates, the three statements inside the block will be executed
Only one time the loop iterates, the three statements inside the block will be executed
None of the above
Compute distance light travels using long variables.
class Light {
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println (distance + “miles.");
}
}
what is the out put of the above program ?
In 100 days light will travel about 16070400000000 miles.
In 1000 days light will travel about 16070400000000 miles.
In 10000 days light will travel about 16070400000000 miles.
In 10 days light will travel about 16070400000000 miles.
LANGUAGE
Demonstrate boolean values.
class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
The output generated by this program is shown here:
b is false
b is true
This is executed.
10 > 9 is true
b is true
b is false
This is executed.
10 > 9 is true
b is false
b is true
This is executed.
10 < 9 is true
b is false
b is NOT true
This is executed.
10 > 9 is true
GUAGE
Demonstrate the basic arithmetic operators.
class BasicMath {
public static void main(String args[]) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
When you run this program, you will see the following output:
Integer Arithmetic Floating Point Arithmetic
a = 3 da = 2.0
b = 7 db = 8.0
c = 3 dd = -0.5
d = 0 de = 0.5
e = 1 dc = 1.5
Integer Arithmetic Floating Point Arithmetic
a = 2 da = 2.0
b = 6 db = 6.0
c = 1 dc = 1.5
d = -1 dd = -0.5
e = 1 de = 0.5
Demonstrate several assignment operators.
class OpEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
what will be the out put of the above program ?
a = 6
b = 8
c = 3
a = 7
b = 8
c = 3
a = 5
b = 8
c = 3
a = 6
b = 9
c = 3
Test for primes.
class FindPrime {
public static void main(String args[]) {
int num;
boolean isPrime = true;
num = 14;
for(int i=2; i <= num/2; i++) {
if((num % i) == 0) {
isPrime = false;
break;}}
if(isPrime) System.out.println("Prime");
else System.out.println("Not Prime");}}
In the above program what is isPrime ?
isPrime is a constant
isPrime is a function
is prime is a procedure
isPrime is a boolean variable
Parts of the for loop can be empty.
class ForVar {
public static void main(String args[]) {
int i;
boolean done = false;
i = 0;
for( ; !done; ) {
System.out.println("i is " + i);
if(i == 10) done = true;
i++;
}
}
}
The above program is valid ? In case of not valid what is the error ?
Valid
Not valid and error is for( ; !done; ) statement
Not valid and error parts of the for statement are empty.
None of the above
/*Here, Box uses a parameterized constructor to
initialize the dimensions of a box.
*/
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;
}
}
class BoxDemo7 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
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 will be the output of the above program ?
Volume is 3000.0
Volume is 162.0
Volume is 300.0
Volume is 162.0
Volume is 3000.0
Volume is 1620.0
None of the above
This class defines an integer stack that can hold 10 values.
class Stack {
int stck[] = new int[10];
int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}AGE
what is Stack( ) in the above program ?
The Stack( ) is a class
The Stack( ) is a constructor
LANGUAGE
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Here is the output of this program:
a = 42
b = 99
a = 99
b = 42
a = 88
b = 99
None of the above