1. Analyze the following code: public class Test { public static void main(String[] args) { double radius; final double PI= 3.15169; double area = radius * radius * PI; System.out.println("Area is " + area); } } a. The program has compile errors because the variable radius is not initialized. b. The program has a compile error because a constant PI is defined inside a method. c. The program has no compile errors but will get a runtime error because radius is not initialized. d. The program compiles and runs fine. 2. Analyze the following code. public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } void xMethod(int n) { n++; } } a. The code has a compile error because xMethod does not return a value. b. The code has a compile error because xMethod is not declared static. c. The code prints n is 1. d. The code prints n is 2. e. The code prints n is 3. 3. What is the output of the second println statement in the main method? public class Foo { int i; static int s; public Foo() { i++; s++; } public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } } a. f2.i is 1 f2.s is 1 b. f2.i is 1 f2.s is 2 c. f2.i is 2 f2.s is 2 d. f2.i is 2 f2.s is 1
AS PER THE BARTELBY GUIDELINES THERE ARE 3 MULTIPLE CHOICE QUESTIONS. PLEASE ANSWER THEM ALL.
1. Analyze the following code:
public class Test {
public static void main(String[] args) {
double radius;
final double PI= 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}
a. The
b. The program has a compile error because a constant PI is defined inside a method.
c. The program has no compile errors but will get a runtime error because radius is
not initialized.
d. The program compiles and runs fine.
2. Analyze the following code.
public class Test {
public static void main(String[] args) {
int n = 2;
xMethod(n);
System.out.println("n is " + n);
}
void xMethod(int n) {
n++;
}
}
a. The code has a compile error because xMethod does not return a value.
b. The code has a compile error because xMethod is not declared static.
c. The code prints n is 1.
d. The code prints n is 2.
e. The code prints n is 3.
3. What is the output of the second println statement in the main method?
public class Foo {
int i;
static int s;
public Foo() {
i++;
s++;
}
public static void main(String[] args) {
Foo f1 = new Foo();
System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);
Foo f2 = new Foo();
System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);
Foo f3 = new Foo();
System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);
}
}
a. f2.i is 1 f2.s is 1
b. f2.i is 1 f2.s is 2
c. f2.i is 2 f2.s is 2
d. f2.i is 2 f2.s is 1
Trending now
This is a popular solution!
Step by step
Solved in 2 steps