Time Remaining :00:00:00

Questions to be answered 15

Java Fundamental Programming Puzzles - Operators And Assignments Puzzle 1



Question No : 1

What will be the output of following program?

class A 
{
    final public int GetResult(int a, int b) { return 0; } 
} 
class B extends A 
{ 
    public int GetResult(int a, int b) {return 1; } 
} 
public class Test 
{
    public static void main(String args[]) 
    { 
        B b = new B(); 
        System.out.println("x = " + b.GetResult(0, 1));  
    } 
}

Question No : 2

What will be the output of the following program?

class TestClass
{
    public static void main(String [] args)
    {
        TestClass obj = new TestClass();
        obj.start();
    }

    void start()
    {
        long [] array1 = {3,4,5};
        long [] array2 = method(array1);
        System.out.print(array1[0] + array1[1] + array1[2] + " ");
        System.out.println(array2[0] + array2[1] + array2[2]);
    }

    long [] method(long [] a3)
    {
        a3[1] = 7;
        return a3;
    }
}

Question No : 3

What will be the output of following program?

  class TestClass
{
    public static void main(String [] args)
    {
        TestClass obj = new TestClass();
        obj.start();
    }

    void start()
    {
        String str1 = "hi";
        String str2 = method(str1);
        System.out.print(str1 + str2);
    }

    String method(String str1)
    {
        str1 = str1 + "hello";
        System.out.print(str1);
        return "hello";
    }
}
  

Question No : 4

What will be the output?

class BitShift 
{
    public static void main(String [] args) 
    {
        int x = 0x80000000;
        System.out.print(x + " and  ");
        x = x >>> 31;
        System.out.println(x);
    }
}

Question No : 5

What will be the output?

class Equals 
{
    public static void main(String [] args) 
    {
        int x = 100;
        double y = 100.1;
        boolean b = (x = y);
        System.out.println(b);
    }
}

Question No : 6

What will be the output?

class Test 
{
    public static void main(String [] args) 
    {
        int x=20;
        String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
        System.out.println(sup);
    }
}

Question No : 7

What will be the output following code?

class Test 
class Test 
{
    public static void main(String [] args) 
    {
        int x= 0;
        int y= 0;
        for (int z = 0; z < 5; z++) 
        {
            if (( ++x > 2 ) && (++y > 2)) 
            {
                x++;
            }
        }
        System.out.println(x + " " + y);
    }
}

Question No : 8

What will be the output following code?

class Test 
class Test 
{
    public static void main(String [] args) 
    {
        int x= 0;
        int y= 0;
        for (int z = 0; z < 5; z++) 
        {
            if (( ++x > 2 ) || (++y > 2)) 
            {
                x++;
            }
        }
    System.out.println(x + " " + y);
    }
}

Question No : 9

What will be the output following code?

class Test 
class Bitwise 
{
    public static void main(String [] args) 
    {
        int x = 11 & 9;
        int y = x ^ 3;
        System.out.println( y | 12 );
    }
}

Question No : 10

What will be the output following code?

class Test 
class SSBool 
{
    public static void main(String [] args) 
    {
        boolean b1 = true;
        boolean b2 = false;
        boolean b3 = true;
        if ( b1 & b2 | b2 & b3 | b2 )
            System.out.print("first ");
        if ( b1 & b2 | b2 & b3 | b2 | b1 )
            System.out.println("second");
    }
}

Question No : 11

What will be the output following code?

class Test 
class Test 
{
    static int s;
    public static void main(String [] args) 
    {
        Test p = new Test();
        p.start();
        System.out.println(s);
    }

    void start() 
    {
        int x = 7;
        twice(x);
        System.out.print(x + " ");
    }

    void twice(int x) 
    {
        x = x*2;
        s = x;
    }
}

Question No : 12

What will be the output following code?

class Test 
class Two 
{
    byte x;
}

class PassO 
{
    public static void main(String [] args) 
    {
        PassO p = new PassO();
        p.start();
    }

    void start() 
    {
        Two t = new Two();
        System.out.print(t.x + " ");
        Two t2 = fix(t);
        System.out.println(t.x + " " + t2.x);
    }

    Two fix(Two tt) 
    {
        tt.x = 42;
        return tt;
    }
}

Question No : 13

What will be the output following code?

class Test 
class BoolArray 
{
    boolean [] b = new boolean[3];
    int count = 0;

    void set(boolean [] x, int i) 
    {
        x[i] = true;
        ++count;
    }

    public static void main(String [] args) 
    {
        BoolArray ba = new BoolArray();
        ba.set(ba.b, 0);
        ba.set(ba.b, 2);
        ba.test();
    }

    void test() 
    {
        if ( b[0] && b[1] | b[2] )
            count++;
        if ( b[1] && b[(++count - 2)] )
            count += 7;
        System.out.println("count = " + count);
    }
}

Question No : 14

What will be the output following code?

class Test 
public class Test 
{ 
    public static void leftshift(int i, int j) 
    {
        i <<= j; 
    } 
    public static void main(String args[]) 
    {
        int i = 4, j = 2; 
        leftshift(i, j); 
        System.out.printIn(i); 
    } 
}

Question No : 15

What will be the output of following program?

public class App 
{
    public static void main( String[] args )
    {
        short x = 1;
        int i = 123456;
        x = x + i;
        System.out.println(x);
    }
}


Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know