• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Java Functions

Question 11

Consider the following JAVA program :
public class First {
                 public static int CBSE (int x) {
                 if (x < 100) x = CBSE (x + 10);
                     return (x – 1);
                        }
      public static void main (String[] args){
      System.out.print(First.CBSE(60));
                         }
                           }
What does this program print ?
  • 59
  • 95
  • 69
  • 99

Question 12

The function f is defined as follows: 

C
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.
1. 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?

  • (i) and (iii)

  • (i) and (iv)

  • (ii) and (iii)

  • (ii) and (iv)

Question 13

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)?

  • 1441

  • 3443

  • 12344321

  • 4321001234

Question 14

Consider the following Java code fragment:
1   public class While
2   {
3    public void loop()
4      {
5      int x = 0;
6       while(1)
7         {
8         System.out.println("x plus one is" +(x+1));
9        }
10     }
11  }
  • There is syntax error in line no. 1
  • There is syntax errors in line nos. 1 & 6
  • There is syntax error in line no. 8
  • There is syntax error in line no. 6

Question 15

What is the result of the following program?
program side-effect (input, output);
    var x, result: integer;
    function f (var x:integer):integer;
    begin
        x:x+1;f:=x;
    end;
begin
    x:=5;
    result:=f(x)*f(x);
    writeln(result);
end;
  • 5
  • 25
  • 36
  • 42

Question 16

Predict the output of the following program. Java
class Test
{
    public void demo(String str)
    {
        String[] arr = str.split(\";\");
        for (String s : arr)
        {
            System.out.println(s);
        }
    }

    public static void main(String[] args)
    {
        char array[] = {\'a\', \'b\', \' \', \'c\', \'d\', \';\', \'e\', \'f\', \' \', 
                        \'g\', \'h\', \';\', \'i\', \'j\', \' \', \'k\', \'l\'};
        String str = new String(array);
        Test obj = new Test();
        obj.demo(str);
    }
}
  • ab cd
    ef gh
    ij kl
  • ab
    cd;ef
    gh;ij
    kl
  • Compilation error

Question 17

Java
class Test {
public static void swap(Integer i, Integer j) {
      Integer temp = new Integer(i);
      i = j;
      j = temp;
   }
   public static void main(String[] args) {
      Integer i = new Integer(10);
      Integer j = new Integer(20);
      swap(i, j);
      System.out.println(\"i = \" + i + \", j = \" + j);
   }
}
  • i = 10, j = 20
  • i = 20, j = 10
  • i = 10, j = 10
  • i = 20, j = 20

There are 17 questions to complete.

Last Updated :
Take a part in the ongoing discussion