Time left:

[All Quizzes] → [Programming With Java] → [Flow Control Structures]


1. ► What would be output of following program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Scanner;

public class FunDo {
    private static Scanner scanInput;
    private static boolean x;

    public static void main(String[]args) {
        if (!x) {
            System.out.println("Main");
        } else {
            scanInput = new Scanner(System.in);
            int i = scanInput.nextInt();

            if (i == 0) {
                System.out.println("A");
            } else if (i < 100 && i > 10) {
                System.out.println("B");

            } else if (i < 1000 && i > 100) {
                System.out.println("C");

            } else if (i < 10000 && i > 1000) {
                System.out.println("D");

            } else {
                System.out.println("XYZ");
            }
        }
    }
}
A.
B.
C.
D.
E.

2. ► What happens when you try to compile following program?

1
2
3
4
5
6
7
8
public class FunDo {                //1
    public static void main(String[]args) {        //2
        int x = 0;                //3
        while (false)
             x = 3;                //4
        if (false)
             x = 5;                //5
} //6 }                                //7
A.
B.
C.
D.
E.

3. ► What would be output of following program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class MyException extends Exception {
    MyException() {
    } MyException(String s) {
        super(s);
    }
}

public class FunDo {
    static void throwException() throws MyException {
        throw new MyException();
    } public static void main(String[]args) {
        try {
            throwException();
        } catch(RuntimeException r) {
            System.out.println("RuntimeException:" + r);
        } catch(MyException exp) {
            System.out.println("MyException");
        }
    }
}
A.
B.
C.
D.
E.

4. ► What would be output of following program?

1
2
3
4
5
public class FunDo {                //1
    public static void main(String[]args) {        //2
        int x = 3;                //3
         System.out.println(x * -3);        //4
} //6 }                                //7
A.
B.
C.
D.
E.

5. ► Which statement(s) are true about following program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class MyException extends Exception {
    MyException() {
    } MyException(String s) {
        super(s);
    }
}

public class FunDo {
    static void throwException() throws MyException {
        throw new MyException();
    } public static void main(String[]args) {
        try {
            System.exit(0);
            throwException();
            return;
        }
        catch(RuntimeException r) {
            System.out.println("RuntimeException:" + r);
        }
        catch(MyException exp) {
            System.out.println("MyException");
            return;
        }
        finally {
            System.out.println("Finally MyException");
        }
    }
}
A.
B.
C.
D.
E.

6. ► What would be output of following program?

1
2
3
4
5
6
7
8
9
10
public class FunOp {
    public static void main(String[]args) {
        int x = 3, y = 5;
        int a = x + y - x / y + 2;
        int b = (x + y) - x / (y + 2);
        String s = null;
        if (b < 8 && Integer.parseInt(s) > 0) {
            System.out.println(a + b);
        }
}}
A.
B.
C.
D.
E.

7. ► What would be output of following program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Fun {
    static void printMsg(int k) {
        switch (k) {
        case 1:
            System.out.println("one");
            break;                // exit the switch
            case 2:System.out.println("two");
            break;                // exit the switch
            case 3:System.out.println("many");
            break;                // not needed, but good style
    }} public static void main(String[]args) {
        printMsg(3);
    }
}
A.
B.
C.
D.
E.

8. ► What would be output of following program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MyException extends Exception {
    MyException() {
    } MyException(String s) {
        super(s);
    }
}

public class FunDo {
    static void throwException() throws MyException {
        throw new MyException();
    } public static void main(String[]args) {
        try {
            throwException();
            return;
        }
        catch(RuntimeException r) {
            System.out.println("RuntimeException:" + r);
        }
        catch(MyException exp) {
            System.out.println("MyException");
        }
    }
}
A.
B.
C.
D.
E.

9. ► What happens when following program is executed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class VirtuQScanner {
    static void throwException() throws Exception {
        throw new Exception();
    } public static void main(String[]args) {
        try {

            System.err.println("Some Error");
            throwException();
            return;
        }
        catch(RuntimeException r) {
            System.out.println("RuntimeException:" + r);
        }
        catch(Exception exp) {
            System.out.println("Exception");
            return;
        }
        finally {
            System.out.println("Finally Exception");
            scanner.close();
        }
    }
}
A.
B.
C.
D.
E.

10. ► Which of following prevents execution of a statement normally?

A.
B.
C.
D.
E.

11. ► Which statement(s) are true about following program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class MyException extends Exception {
    MyException() {
    } MyException(String s) {
        super(s);
    }
}

public class FunDo {
    static void throwException() throws MyException {
        throw new MyException();
    } public static void main(String[]args) {
        try {
            throwException();
        } catch(RuntimeException r) {
            System.out.println("RuntimeException:" + r);
        } catch(MyException exp) {
            System.out.println("MyException");
            return;
        }
        finally {
            System.out.println("Finally MyException");
        }
    }
}
A.
B.
C.
D.
E.

12. ► Refer following code carefully and select correct statement(s). The class VirtuQScanner when executed after compilation expects a user input and gives output accordingly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Scanner;

public class VirtuQScanner {
    private static Scanner scanInput;
    private static boolean x;

    public static void main(String[]args) {
        if (x) {
            System.out.println("Main");
        };

        scanInput = new Scanner(System.in);
        int i = scanInput.nextInt();

        if (i == 0) {
            System.out.println("A");
        } else if (i < 100 && i > 10) {
            System.out.println("B");

        } else if (i < 1000 && i > 100) {
            System.out.println("C");

        } else if (i < 10000 && i > 1000) {
            System.out.println("D");

        } else {
            System.out.println("XYZ");
        }

    }
}
A.
B.
C.
D.
E.


© 2016 VirtuQ™ Education All right reserved. | Terms of Use | Privacy Policy