Thursday, April 9, 2020

Java Output Questions set - 2


Set -2

1)class Output
{
    final static short i = 2;
    public static int j = 0;

    public static void main(String [] args)
    {
        for (int k = 0; k < 3; k++)
        {
            switch (k)
            {
                case i: System.out.print(" 0 ");
                case i-1: System.out.print(" 1 ");
                case i-2: System.out.print(" 2 ");
            }
        }
    }
}

A.210100
B.012122
C.212012
D.012
E.Compile error

2)public class Final
{
    final int assign;

    public static void main(String[] args)
    {
        final int result = 20;

        Final f = new Final();
        f.assign = process(result);
        System.out.println(f.assign);
    }

    final static int process(int a)
    {
        return a + 5;
    }
}

A.20
B.25
C.Compile Error
D.Runtime Error

3)String x = new String("xyz");
String y = "abc";
x = x + y;

How many String objects have been created?
A. 2
B. 3
C. 4
D. 5

4)public class ObjComp
{
    public static void main(String [] args )
    {
        int result = 0;
        ObjComp oc = new ObjComp();
        Object o = oc;

        if (o == oc)
            result = 1;
        if (o != oc)
            result = result + 10;
        if (o.equals(oc) )
            result = result + 100;
        if (oc.equals(o) )
            result = result + 1000;

        System.out.println("result = " + result);
    }
}

A. 1
B. 10
C. 101
D. 1101

5)public class Example
{
    public static void main(String [] args)
    {
        double values[] = {-2.3, -1.0, 0.25, 4};
        int cnt = 0;
        for (int x=0; x < values.length; x++)
        {
            if (Math.round(values[x] + .5) == Math.ceil(values[x]))
            {
                ++cnt;
            }
        }
        System.out.println("same results " + cnt + " time(s)");
    }
}

A. same results 0 time(s)
B. same results 2 time(s)
C. same results 4 time(s)
D. Compilation fails.

6)try
{
    Float f1 = new Float("3.0");
    int x = f1.intValue();
    byte b = f1.byteValue();
    double d = f1.doubleValue();
    System.out.println(x + b + d);
}
catch (NumberFormatException e) /* Line 9 */
{
    System.out.println("bad number"); /* Line 11 */
}

A. 9.0
B. bad number
C. Compilation fails on line 9.
D. Compilation fails on line 11.


7)String s = "ABC";
s.toLowerCase();
s += "def";
System.out.println(s);

A. ABC
B. abc
C. ABCdef
D. Compile Error


8)class Tree { }
class Pine extends Tree { }
class Oak extends Tree { }
public class Forest1
{
    public static void main (String [] args)
    {
        Tree tree = new Pine();
        if( tree instanceof Pine )
            System.out.println ("Pine");
        else if( tree instanceof Tree )
            System.out.println ("Tree");
        else if( tree instanceof Oak )
            System.out.println ( "Oak" );
        else
            System.out.println ("Oops ");
    }
}

A. Pine
B. Tree
C. Forest
D. Oops

9)
String a = "newspaper";
a = a.substring(5,7);
char b = a.charAt(1);
a = a + b;
System.out.println(a);

A. apa
B. app
C. apea
D. apep

10)class MainClass
{
 public static void main(String arg[])
 {
  int arr[][]={{4,3},{2,1}};
  int i,j;
  for(i=1;i>-1;i--)
  {
   for(j=1;j>-1;j--)
   {
    System.out.print(arr[i][j]);
   }
  }
 }
}


A.1234
B.12
C.34
D.1245






























No comments:

Post a Comment

Python Technical Interview Questions

 1)first non repeating character Malayalam y programming p str = "MalayalaM" a = '\0' for c in str:     if str.index(c) ==...