You are on page 1of 16

class Test

{
String str = "a";

void A()
{
try
{
str +="b";
B();
}
catch (Exception e)
{
str += "c";
}
}

void B() throws Exception


{
try
{
str += "d";
C();
}
catch(Exception e)
{
throw new Exception();
}
finally
{
str += "e";
}

str += "f";

void C() throws Exception


{
throw new Exception();
}

void display()
{
System.out.println(str);
}

public static void main(String[] args)


{
Test object = new Test();
object.A();
object.display();
}

Answer: abdec

Explanation: ‘throw’ keyword is used to explicitly throw an exception.


finally block is always executed even when an exception occurs.
Call to method C() throws an exception. Thus, control goes in catch block of
method B() which again throws an exception. So, control goes in catch block of
method A().

Say that methodA calls methodB, and methodB calls methodC. MethodC might throw a

NumberFormatException. Can the program be written so that methodA handles the exception?

a. No, the exception must be handled by methodC or its caller, methodB.

b. No, methodC must handle the exception.

c. Yes, if the header for methodC says ...throws NumberFormatException

d. Yes, if the headers for methodC and methodB say ...throws NumberFormatException

Answer: D. Yes, if the headers for methodC and methodB say ...throws NumberFormatException

What is the only type of exception that is NOT checked?

a. Class Exception.

b. Class RunTimeException and its subclasses.

c. Class IOException and its subclasses.

d. Class ArithmeticException only.

Answer: B. Class RunTimeException and its subclasses.


Output of following Java program?

class Main {

public static void main(String args[]) {

int x = 0;

int y = 10;

int z = y/x;

Answer: Compiles fine but throws ArithmeticException exception

Exception is a class/interface/abstract class/other?

a.Class

b.Interface

c.Abstract class

d.Other

Answer: A. Class

Exception is found in which package in java

a.java.lang

b.java.util

c.java.io

d.java

Answer: A. java.lang

What keyword is used to explicitly raise a exception?

a.catch

b.throw
c.throws

d.raise

Answer: D. raise

Which method of the "Exception" class prints a list of methods that were called before the exception
was thrown? Please select the correct option "A, B, C, D, or E"

A. printErrors()

B. getMessage()

C. printStackTrace()

D. traceStack()

E. toString()

Answer: C. printStackTrace()

public class Main {

public static void main(String args[]) {

String x = null;

giveMeAString(x);

System.out.println(x);

static void giveMeAString(String y)

y = "GeeksQuiz";

A. GeeksQuiz
B. Null
C. Compiler error
D. Exception

Answer: B. null
What is the output of this main() method? Put the letters into the answer box provided with no spaces
between (i.e. ABCEDFG)

public static void main(String [] args) {

int value;

System.out.print("@");

v = getRemoteId();

System.out.print(v);

System.out.print("E");

v = getStudentCount();

System.out.print(v);

System.out.print("F");

v = getMonitorCount();

System.out.print("W");

public static int getRemoteId() {

return 3;

public static int getMonitorCount() {

return 2;

public static int getStudentCount() {

return 6;

Answer: @3E6FW

It refers to the part of function heading that specifies an input for the function to perform its task.

A. Global variable
B. Function arguments or function parameters
C. Function statements
D. Function heading
Answer: B. Function arguments or function parameters

it is the statement that indicates a function will be used.

A. function declaration
B. function call
C. function definition
D. function name

Answer: B. function call

It is a statement that tells the compiler about a function's name, return type, and parameters

A. function definition
B. function call
C. function declaration
D. function name

Answer: A. function definition

It is a part of a function header that identifies the function

A. function definition
B. function call
C. function declaration
D. function name

Answer: D. function name

This method copies the actual value of an argument into the formal parameter of the function.

A. Call by value
B. Call by reference
C. Call by pointer

Answer. A. Call by value

To call the function startCall() with the number 4432 and 3 retries what would you write in the main()
function? Remember to put a ; at the end of the statement.
void startCall(int number, int retries)

// code is hidden

Answer: startCall(4432, 3);

Predict the output of the following program.

class Test

public static void main(String[] args)

String str = "geeks";

str.toUpperCase();

str += "forgeeks";

String string = str.substring(2,13);

string = string + str.charAt(4);;

System.out.println(string);

(A) eksforgeekss

(B) eksforgeeks

(C) EKSforgeekss

(D) EKSforgeeks

Answer: (A) eksforgeekss


Explanation: str.toUpperCase() returns ‘str’ in upper case. But,it does not change the original string ‘str’.

str.substring(x, y) returns a string from position ‘x'(inclusive) to position ‘y'(exclusive).

str.charAt(x) returns a character at position ‘x’ in the string ‘str’.

Study the following program:

//precondition: x>=0

public void demo(int x)

System.out.print(x % 10);

if (x % 10 != 0)

demo(x/10);

System.out.print(x%10);

Which of the following is printed as a result of the call demo(1234)?

(A) 1441

(B) 3443

(C) 12344321

(D) 43211234

Answer: (D) 43211234

Explanation: In the above code, first print statement is executed and prints the value obtained after
performing modulus of 10 and the recursively another function is called with the value divide by 10. And
after the return of the function, it prints the values again.
demo(1234)

prints 4 call demo(123)

prints 3 call demo(12)

prints 2 call demo(1)

prints 1 call demo (0)

prints 1 prints 2 prints 3 prints 4.

Thus answer is D.

What are two ways to view recursion?

A. (i) static view, and (ii) dynamic view.

B. (i) recursive view, and (ii) iterative view.

C. (i) math view, and (ii) programming view

D. (i) code view, and (ii) translation view

Answer: A. (i) static view, and (ii) dynamic view.

Look at square numbers one more time:

square(1) = 1

square(N) = square(N-1) + 2N -1

Assume the definition has been implemented correctly. How many activations will there be on the
activation chain if main() calls square(5)?

A. 1

B. 3

C. 5

D. 6

Answer: C. 5

Consider the following recursive C function. If get(6) function is being called in main() then how many
times will the get() function be invoked before returning to the main()?

void get (int n)


{

if (n < 1) return;

get(n-1);

get(n-3);

printf("%d", n);

(A) 15

(B) 25

(C) 35

(D) 45

Answer: (B) 25

Consider the C function given below.

int f(int j)

static int i = 50;

int k;

if (i == j)

printf(“something”);

k = f(i);

return 0;

else return 0;

Which one of the following is TRUE?


(A) The function returns 0 for all values of j.

(B) The function prints the string something for all values of j.

(C) The function returns 0 when j = 50.

(D) The function will exhaust the runtime stack or run into an infinite loop when j = 50

Answer: (D) The function will exhaust the runtime stack or run into an infinite loop when j = 50

Explanation: When j is 50, the function would call itself again and again as neither i nor j is changed inside
the recursion.

Consider the following C function.

int fun (int n)

int x=1, k;

if (n==1) return x;

for (k=1; k < n; ++k)

x = x + fun(k) * fun(n – k);

return x;

The return value of fun(5) is __________.

(A) 0

(B) 26

(C) 51

(D) 71

Answer: (C) 51

Explanation:

fun(5) = 1 + fun(1) * fun(4) + fun(2) * fun(3) +

fun(3) * fun(2) + fun(4) * fun(1)

= 1 + 2*[fun(1)*fun(4) + fun(2)*fun(3)]
Substituting fun(1) = 1

= 1 + 2*[fun(4) + fun(2)*fun(3)]

Calculating fun(2), fun(3) and fun(4)

fun(2) = 1 + fun(1)*fun(1) = 1 + 1*1 = 2

fun(3) = 1 + 2*fun(1)*fun(2) = 1 + 2*1*2 = 5

fun(4) = 1 + 2*fun(1)*fun(3) + fun(2)*fun(2)

= 1 + 2*1*5 + 2*2 = 15

Substituting values of fun(2), fun(3) and fun(4)

fun(5) = 1 + 2*[15 + 2*5] = 51

Consider the following C function:

int f(int n)

static int i = 1;

if (n >= 5)

return n;

n = n+i;

i++;

return f(n);

The value returned by f(1) is

(A) 5

(B) 6

(C) 7
(D) 8

Answer: (C) 7

What does fun2() do in general?

int fun(int x, int y)

if (y == 0) return 0;

return (x + fun(x, y-1));

int fun2(int a, int b)

if (b == 0) return 1;

return fun(a, fun2(a, b-1));

(A) x*y

(B) x+x*y

(C) xy

(D) yx

Answer: (C) xy

Explanation: The function multiplies x to itself y times which is xy.

What will be the output of the following C program?

void count(int n)

static int d = 1;

printf("%d ", n);

printf("%d ", d);

d++;
if(n > 1) count(n-1);

printf("%d ", d);

int main()

count(3);

(A) 3 1 2 2 1 3 4 4 4

(B) 3 1 2 1 1 1 2 2 2

(C) 3 1 2 2 1 3 4

(D) 3 1 2 1 1 1 2

Answer: (A) 3 1 2 2 1 3 4 4 4

Explanation:

count(3) will print value of n and d. So 3 1 will be printed

and d will become 2.

Then count(2) will be called. It will print value of n and d.

So 2 2 will be printed and d will become 3.

Then count(1) will be called. It will print value of n and d.

So 1 3 will be printed and d will become 4.

Now count(1) will print value of d which is 4. count(1) will

finish its execution.

Then count(2) will print value of d which is 4.

Similarly, count(3) will print value of d which is 4.

So series will be A.

The function f is defined as follows:

int f (int n) {

if (n <= 1) return 1;

else if (n % 2 == 0) return f(n/2);


else return f(3n - 1);

Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the
following statements.

i. The function f terminates for finitely many different values of n ≥ 1.

ii. The function f terminates for infinitely many different values of n ≥ 1.

iii. The function f does not terminate for finitely many different values of n ≥ 1.

iv. The function f does not terminate for infinitely many different values of n ≥ 1.

Which one of the following options is true of the above?

(A) (i) and (iii)

(B) (i) and (iv)

(C) (ii) and (iii)

(D) (ii) and (iv)

Answer: (D) (ii) and (iv)

Explanation: The function terminates for all values having a factor of 2 {(2.x)2==0}

So, (i) is false and (ii) is TRUE.

Let n = 3, it will terminate in 2nd iteration.

Let n=5, it will go like 5 – 14 – 7 – 20 – 10 – 5 – and now it will repeat.

And any number with a factor of 5 and 2, there are infinite recursions possible.

So, (iv) is TRUE and (iii) is false.

Consider the following recursive function fun(x, y). What is the value of fun(4, 3)

int fun(int x, int y)

if (x == 0)

return y;

return fun(x - 1, x + y);

A. 13
B. 12
C. 9
D. 10

Answer: A. 13

You might also like