Java Functions

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


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"; 
    } 
}
Cross
GeeksQuiz
Tick
null
Cross
Compiler Error
Cross
Exception


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);
   }
}
Tick
i = 10, j = 20
Cross
i = 20, j = 10
Cross
i = 10, j = 10
Cross
i = 20, j = 20


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;
    }
}
Tick
i.x = 20, j.x = 10
Cross
i.x = 10, j.x = 20
Cross
i.x = 10, j.x = 10
Cross
i.x = 20, j.x = 20


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++?
Cross
Yes
Tick
No


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;
    }
}
Cross
0
Cross
Garbage Value
Tick
Compiler Error
Cross
Runtime Error


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);
    }
}

Tick
ab cd
ef gh
ij kl
Cross
ab
cd;ef
gh;ij
kl
Cross
Compilation error


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);
    }
}
Cross
gsforgeeks
Tick
gksforgeeks
Cross
geksforgeeks
Cross
Compilation error


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");
    }
}

Tick
hashCode of object1 is equal to object2
value of object1 is equal to object2
Cross
hashCode of object1 is equal to object2
memory address of object1 is same as object2
value of object1 is equal to object2
Cross
memory address of object1 is same as object2
value of object1 is equal to object2


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);
    }
}

Tick
obj1.a = 10
obj2.a = 20
Cross
obj1.a = 20
obj2.a = 20
Cross
obj1.a = 10
obj2.a = 10


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.


  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads