Java Functions

    Last Updated :
    Discuss
    Comments

    Question 1

    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(3 * n - 1);
    }
    
    C
    int f (int n) {
        if (n <= 1) return 1;
        else if (n % 2  ==  0) return f(n/2);
        else return f(3n - 1);
    }
    
    Java
    int f(int n) {
        if (n <= 1) return 1;
        else if (n % 2 == 0) return f(n / 2);
        else return f(3 * n - 1);
    }
    
    Python
    def f(n):
        if n <= 1:
            return 1
        elif n % 2 == 0:
            return f(n // 2)
        else:
            return f(3 * n - 1)
    
    JavaScript
    function f(n) {
        if (n <= 1) return 1;
        else if (n % 2 === 0) return f(n / 2);
        else return f(3 * n - 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 2

    Predict the output of the following program. Java
    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);
        }
    }
    
    • eksforgeekss
    • eksforgeeks
    • EKSforgeekss
    • EKSforgeeks

    Question 3

    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

    Question 4

    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 5

    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 6

    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 7

    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 8

    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 9

    Given the array of integers ‘array’ shown below : 13, 7, 27, 2, 18, 33, 9, 11, 22, 8. What is the output of the following JAVA statements ?
    int [ ] p = new int [10];
    int [ ] q = new int [10];
    for (int k = 0; k < 10; k ++)
    p[k] = array [k];
    q = p;
    p[4] = 20;
    System.out.println(array [4] + “ : ” + q[4]);
    • 20 : 20
    • 18 : 18
    • 18 : 20
    • 20 : 18

    Question 10

    Output of following Java program? C
    class Main {
        public static void main(String args[]) {   
                 System.out.println(fun());
        } 
     
        int fun()
        {
          return 20;
        }
    }
    
    • 20
    • compiler error
    • 0
    • garbage value

    There are 17 questions to complete.

    Take a part in the ongoing discussion