Java Functions

  • Last Updated : 03 Dec, 2021

12
Question 1
Output of following Java program?
class Main {
    public static void main(String args[]) {   
             System.out.println(fun());
    } 
 
    int fun()
    {
      return 20;
    }
}
A
20
B
compiler error
C
0
D
garbage value
Java Functions    
Discuss it


Question 1 Explanation: 
main() is a static method and fun() is a non-static method in class Main. Like C++, in Java calling a non-static function inside a static function is not allowed
Question 2
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
Java Functions    
Discuss it


Question 2 Explanation: 
Parameters in Java is passed by value. So the changes made to y do not reflect in main().
Question 3
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);
   }
}
A
i = 10, j = 20
B
i = 20, j = 10
C
i = 10, j = 10
D
i = 20, j = 20
Java Functions    50 Java Language MCQs with Answers    
Discuss it


Question 3 Explanation: 
Parameters are passed by value in Java
Question 4
class intWrap {
   int x;
} 
public class Main { 
    public static void main(String[] args) {
       intWrap i = new intWrap();
       i.x = 10;
       intWrap j = new intWrap();
       j.x = 20;
       swap(i, j);
       System.out.println("i.x = " + i.x + ", j.x = " + j.x);
    } 
    public static void swap(intWrap i, intWrap j) {
       int temp = i.x;
       i.x = j.x;
       j.x = temp;
    }
}
A
i.x = 20, j.x = 10
B
i.x = 10, j.x = 20
C
i.x = 10, j.x = 10
D
i.x = 20, j.x = 20
Java Functions    
Discuss it


Question 4 Explanation: 
Objects are never passed at all. Only references are passed. The values of variables are always primitives or references, never objects
Question 5
In Java, can we make functions inline like C++?
A
Yes
B
No
Java Functions    
Discuss it


Question 5 Explanation: 
Unlike C++, in Java we can't suggest functions as inline. Java compiler does some complicated analysis and may make functions inline internally.
Question 6
Predict the output?
class Main {
    public static void main(String args[]) {   
             System.out.println(fun());
    }   
    static int fun(int x = 0)
    {
      return x;
    }
}
A
0
B
Garbage Value
C
Compiler Error
D
Runtime Error
Java Functions    
Discuss it


Question 6 Explanation: 
Java doesn't support default arguments. In Java, we must write two different functions.
Question 7
Predict the output of the following program.
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);
    }
}

A
ab cd
ef gh
ij kl
B
ab
cd;ef
gh;ij
kl
C
Compilation error
Java Functions    50 Java Language MCQs with Answers    
Discuss it


Question 7 Explanation: 

Class String has a inbuilt parameterized constructor String(character_array) which initializes string 'str' with the values stored in character array. The split() method splits the string based on the given regular expression or delimiter passed it as parameter and returns an array.
Question 8
Predict the output of the following program.

class Test
{
    public static void main(String[] args)
    {
        StringBuffer a = new StringBuffer("geeks");
        StringBuffer b = new StringBuffer("forgeeks");
        a.delete(1,3);
        a.append(b);
        System.out.println(a);
    }
}
A
gsforgeeks
B
gksforgeeks
C
geksforgeeks
D
Compilation error
Java Functions    
Discuss it


Question 8 Explanation: 

delete(x, y) function deletes the elements from the string at position ‘x’ to position ‘y-1’. append() function concatenates the second string to the first string.
Question 9
Predict the output of the following program.
 class Test
{

    public static void main(String[] args)
    {
        String obj1 = new String("geeks");
        String obj2 = new String("geeks");

        if(obj1.hashCode() == obj2.hashCode())
            System.out.println("hashCode of object1 is equal to object2");

        if(obj1 == obj2)
            System.out.println("memory address of object1 is same as object2");

        if(obj1.equals(obj2))
            System.out.println("value of object1 is equal to object2");
    }
}

A
hashCode of object1 is equal to object2
value of object1 is equal to object2
B
hashCode of object1 is equal to object2
memory address of object1 is same as object2
value of object1 is equal to object2
C
memory address of object1 is same as object2
value of object1 is equal to object2
Java Functions    
Discuss it


Question 9 Explanation: 

obj.hashCode() function returns a 32-bit hash code value for the object ‘obj’. obj1.equals(obj2) function returns true if value of obj1 is equal to obj2. obj1 == obj2 returns true if obj1 refers to same memory address as obj2.
Question 10
Predict the output of the following program.
 class Test implements Cloneable
{
    int a;

    Test cloning()
    {
        try
        {
            return (Test) super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            System.out.println("CloneNotSupportedException is caught");
            return this;
        }
    }
}

class demo
{

    public static void main(String args[])
    {
        Test obj1 = new Test();
        Test obj2;
        obj1.a = 10;
        obj2 = obj1.cloning();
        obj2.a = 20;

        System.out.println("obj1.a = " + obj1.a);
        System.out.println("obj2.a = " + obj2.a);
    }
}

A
obj1.a = 10
obj2.a = 20
B
obj1.a = 20
obj2.a = 20
C
obj1.a = 10
obj2.a = 10
Java Functions    
Discuss it


Question 10 Explanation: 
The clone( ) method generates a duplicate copy of the object on which it is called. Only classes that implement the Cloneable interface can be cloned.
There are 17 questions to complete.
12
My Personal Notes arrow_drop_up