• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
Switch to Dark Mode

Java Functions

Last Updated : Mar 22, 2024
Discuss
Comments

Question 1

Output of following Java program? C
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

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"; 
    } 
}
  • A
    GeeksQuiz
  • B
    null
  • C
    Compiler Error
  • D
    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;
    }
}
  • 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

Question 4

In Java, can we make functions inline like C++?
  • A
    Yes
  • B
    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;
    }
}
  • A
    0
  • B
    Garbage Value
  • C
    Compiler Error
  • D
    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);
    }
}
  • A
    gsforgeeks
  • B
    gksforgeeks
  • C
    geksforgeeks
  • D
    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");
    }
}
  • 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

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);
    }
}
  • A
    obj1.a = 10
    obj2.a = 20
  • B
    obj1.a = 20
    obj2.a = 20
  • C
    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);
    }
}
  • A
    eksforgeekss
  • B
    eksforgeeks
  • C
    EKSforgeekss
  • D
    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]);
  • A
    20 : 20
  • B
    18 : 18
  • C
    18 : 20
  • D
    20 : 18

Master JAVA and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!

There are 17 questions to complete.

Take a part in the ongoing discussion

It seems that you are using an ad blocker.
Please disable it to support us!

Continue without supporting 😢