• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Java Functions

Question 1

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

Question 2

C
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\"; 
    } 
}
  • GeeksQuiz
  • null
  • Compiler Error
  • Exception

Question 3

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

Question 4

In Java, can we make functions inline like C++?
  • Yes
  • No

Question 5

Predict the output? Java
class Main {
    public static void main(String args[]) {   
             System.out.println(fun());
    }   
    static int fun(int x = 0)
    {
      return x;
    }
}
  • 0
  • Garbage Value
  • Compiler Error
  • Runtime Error

Question 6

Predict the output of the following program. Java
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);
    }
}
  • gsforgeeks
  • gksforgeeks
  • geksforgeeks
  • Compilation error

Question 7

Predict the output of the following program. Java
 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\");
    }
}
  • hashCode of object1 is equal to object2
    value of object1 is equal to object2
  • hashCode of object1 is equal to object2
    memory address of object1 is same as object2
    value of object1 is equal to object2
  • memory address of object1 is same as object2
    value of object1 is equal to object2

Question 8

Predict the output of the following program. Java
 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);
    }
}
  • obj1.a = 10
    obj2.a = 20
  • obj1.a = 20
    obj2.a = 20
  • obj1.a = 10
    obj2.a = 10

Question 9

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 10

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

There are 17 questions to complete.

Last Updated :
Take a part in the ongoing discussion