CoreJavaMidterms

Is this your test? Login to manage it. If not, you can generate an exam just like it.

This is a non-interactive preview of the quiz content.

1.
4 points
public class Example8 {
public static void main(String[] args) {
System.out.println(check(null, 3));
System.out.println(check("hello", 7));
System.out.println(check("bye", 2));
System.out.println(check("hai", 3));
}

public static int check(String a, int n) {
if (n == 7)
return n;
else if (n == 3) {
if (a != null)
return 5;
} else if (n == 5 && a != null) {
if (a.equals("hai"))
return 3;
else if (a.equals("bye"))
return 4;
}
return -3;
}
}
2.
4 points
public class Test {
public static void main(String[] args) {
int new = 3;
System.out.println(new);
}
}

This code will compile properly and display "3". True or False?
3.
4 points
public class Example13 {
public static void main(String[] args) throws Exception {
int a = 30;
while (a) {
System.out.print("value:" + a--);
}
}
}
4.
2 points
1. public class Question2 {
2. public static void main(String[] args){
3. int x,y,z;
4. y=1;
5. x=z=1;
6. System.out.println(y+","+x+","+z);
}
}
5.
3 points
public class Sample {
int x = 100;

public void checkValue() {
x = 10;
x += 1;
System.out.println(x++);
}

public static void main(String args[]) {
Sample t = new Sample();
t.x = 5;
t.checkValue();
}
}
6.
3 points
1 public class Sample{
2 public static void main(String args[]) {
3 int i = 1, j = 2;
4 i++;
5 j--;
6 if (i / j > 1)
7 i++;
8 System.out.println(i + j);
}}
7.
3 points
1 public class Sample {
2 public static void main(String args[]) {
3 int k=1, m=3;
4 do {
5 k=++k;
6 m--;
7 } while(m>0);
8 System.out.println(k);
9 }}
8.
2 points
public class test {
public static void main(String args[]) {
int i, j=1;
i = (j>1)?2:1;
switch(i) {
case 0: System.out.println(0); break;
case 1: System.out.println(1); break
case 2: System.out.println(2); break;
case 3: System.out.println(3); break;
}
}
}
9.
4 points
1 public class Test {
2 public static void main(String[] args) {
3 int x = 4;
4 boolean bl1 = false;
5 boolean bl2 = true;

6 if ((x == 3) && !bl2)
7 System.out.print("hello");
8 System.out.print("hi");

9 if ((bl2 = true) && bl1)
10 System.out.print("bye");
}
}
10.
2 points
1 public class Test {
2 public static void main(String args[]) {
3 int i, j, k;
4 for (i = 0; i < 4; i++) {
5 for (j = 1; j < 3; j++) {
6 for (k = 2; k < 3; k++) {
7 if ((i == j) && (j != k))
8 System.out.println(i);
}}}
}}
11.
3 points
1 .public class LoopOutput{
2. public static void main(String args[]) {
3. int a = 4;
4. int b = 3;
5. int c = 2;
6. for(;b < 7;b++) {
7. if(c++ > a) {
8. a = b + c;
9. }
11. }
12. System.out.println("Value of C is " + c);
13. }
14. }
12.
3 points
1 public class Sample {
2 public static void main(String args[]) {
3 int i = 1, j = 2;
4 i++;
5 j--;
6 if (i % j > 1)
7 i++;
8 System.out.println("i = "+i+ " j = "+j);
}}
13.
2 points
public class SampleCode {
public static void main(String args[]){
SampleCode sc = new SampleCode();
sc.meth();
}
public void meth(){
int arr[] = new int[] {1,2,3,4};
System.out.print(arr.length);
}
}
14.
3 points
public class Test {
public static void main(String args[]) {
int i = 9;
while (i++ <= 12) {
i++;
}
System.out.print(i);
}
}
15.
2 points
public class Sample {
public static void main(String args[]){
int[] x = new int[5];
System.out.println(x[5]);
}
}
16.
4 points
public class RoseindiaSample {
public static void main(String[] args) {
int k=5;
System.out.println(getBoolean()? k=3*k++:k+++ ++k);
}

public static boolean getBoolean(){
if((int)(Math.random()*3)==0)
return false;
else
return true;
}
}
17.
2 points
1 public class Test {
2 public static void main(String args[]) {
3 int i, j ;
4 for (i = 0; i < 3; i++) {
5 for (j = 1; j < 4; j++) {
6 i %= j;
7 System.out.print(j);
}}
}}
18.
2 points
An empty string is the same as a null reference.
19.
2 points
public class Test1 {
public static void main(String[] args) {
int x = 1;
Test1 p = new Test1();
p.plusX(x);
System.out.print(" main x = " + ++x);
}

void plusX(int x) {
System.out.print(" plusX x = " + x++);
}
}
20.
2 points
public class Sample {
int x = 100;

public void checkValue(int x) {
x += 1;
System.out.println(x++);
}

public static void main(String args[]) {
Sample t = new Sample ();
t.checkValue(5);
}
}
21.
2 points
1 public class Sample1 {
2 public static void main(String[] args) {
3 for (int i = 0; i < 2; i++) {
4 System.out.println(getValue(101));
}
}

5 public static int getValue(int i) {
6 return (short) (Math.random() * i);
}
}
22.
2 points
1 package testpackage;

2 public class ClassA {

3 public static void doSomething() {
4 System.out.println("do something");
}}

5 // Place the statement here

6 public class ClassB {
7 public static void main(String args[]) {
8 ClassA.doSomething();
}}

What statement you place at line number 5 for successful compilation and execution of above code?
23.
4 points
1 public class Test {
2 public static void main(String args[]) {
3 int i = 5;
4 do {
5 i--;
6 } while (i > 2);
7 System.out.println(i);
8 }}
24.
2 points
All comparisons involving NaN and a non-NaN always result in false.
25.
2 points
1. public class Example1 {
2.
3. public static void main(String[] args) {
4.
5. System.out.println("To print x you need " + x);
}
}
26.
2 points
1 public class Example20{
2 public static void main(String[] args){
3 int a = 0;
4 int b=2;
5 int c = 3;
6 if(a++) {
7 b = a * c;
8 a /= 2;
9 }else {
10 b = b * b;
11 ++c;
12}}}