Java Programming

Java Programming

Language Fundamentals has 23 Questions
Which four options describe the correct default values for array elements of the types indicated?
  1. int -> 0
  2. String -> "null"
  3. Dog -> null
  4. char -> 'u0000'
  5. float -> 0.0f
  6. boolean -> true

1, 2, 3, 4
1, 3, 4, 5
2, 4, 5, 6
3, 4, 5, 6
What will be the output of the program?
public class CommandArgsThree 
{
    public static void main(String [] args) 
    {
        String [][] argCopy = new String[2][2];
        int x;
        argCopy[0] = args;
        x = argCopy[0].length;
        for (int y = 0; y < x; y++) 
        {
            System.out.print(" " + argCopy[0][y]);
        }
    }
}

and the command-line invocation is


0 0
1 2
0 0 0
1 2 3
Which one of these lists contains only Java programming language keywords?

class, if, void, long, Int, continue
goto, instanceof, native, finally, default, throws
try, virtual, throw, final, volatile, transient
strictfp, constant, super, implements, do
What will be the output of the program?
public class CommandArgs 
{
    public static void main(String [] args) 
    {
        String s1 = args[1];
        String s2 = args[2];
        String s3 = args[3];
        String s4 = args[4];
        System.out.print(" args[2] = " + s2);
    }
}

and the command-line invocation is


args[2] = 2
args[2] = 3
args[2] = null
An exception is thrown at runtime.
Which will legally declare, construct, and initialize an array?

int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};
public class F0091 
{    
    public void main( String[] args ) 
    {  
        System.out.println( "Hello" + args[0] ); 
    } 
}

What will be the output of the program, if this code is executed with the command line:


Hello
Hello Foo91
Hello world
The code does not run.
Which is a reserved word in the Java programming language?

method
native
subclasses
reference
What will be the output of the program?

public class TestDogs 
{
    public static void main(String [] args) 
    {
        Dog [][] theDogs = new Dog[3][];
        System.out.println(theDogs[2][0].toString());
    }
}
class Dog { }

null
theDogs
Compilation fails
An exception is thrown at runtime
Which is a valid keyword in java?

interface
string
Float
unsigned
What will be the output of the program ?
public class Test 
{
    public static void main(String [] args) 
    {
        signed int x = 10;
        for (int y=0; y<5; y++, x--)
            System.out.print(x + ", ");
    }
}

10, 9, 8, 7, 6,
9, 8, 7, 6, 5,
Compilation fails.
An exception is thrown at runtime.
Which three are legal array declarations?
  1. int [] myScores [];
  2. char [] myChars;
  3. int [6] myScores;
  4. Dog myDogs [];
  5. Dog myDogs [7];

1, 2, 4
2, 4, 5
2, 3, 4
All are correct.
What will be the output of the program?
public class CommandArgsTwo 
{
    public static void main(String [] argh) 
    {
        String [] args;
        int x;
        x = argh.length;
        for (int y = 1; y <= x; y++) 
        {
            System.out.print(" " + argh[y]);
        }
    }
}

and the command-line invocation is


0 1 2
1 2 3
0 0 0
An exception is thrown at runtime
public interface Foo 
{ 
    int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
  1. final int k = 4;
  2. public int k = 4;
  3. static int k = 4;
  4. abstract int k = 4;
  5. volatile int k = 4;
  6. protected int k = 4;

1, 2 and 3
2, 3 and 4
3, 4 and 5
4, 5 and 6
In the given program, how many lines of output will be produced?
public class Test 
{
    public static void main(String [] args) 
    {
    int [] [] [] x = new int [3] [] [];
    int i, j;
    x[0] = new int[4][];
    x[1] = new int[2][];
    x[2] = new int[5][];
    for (i = 0; i < x.length; i++)
    {
        for (j = 0; j < x[i].length; j++) 
        {
            x[i][j] = new int [i + j + 1];
            System.out.println("size = " + x[i][j].length);
        }
    }
    }
}

7
9
11
13
Which one of the following will declare an array and initialize it with five numbers?

Array a = new Array(5);
int [] a = {23,22,21,20,19};
int a [] = new int(5);
int [5] array;
What will be the output of the program?
public class X 
{
    public static void main(String [] args) 
    {
        String names [] = new String[5];
        for (int x=0; x < args.length; x++)
            names[x] = args[x];
        System.out.println(names[2]);
    }
}

and the command line invocation is


names
null
Compilation fails
An exception is thrown at runtime
Which three are valid declarations of a char?
  1. char c1 = 064770;
  2. char c2 = 'face';
  3. char c3 = 0xbeef;
  4. char c4 = u0022;
  5. char c5 = 'iface';
  6. char c6 = 'uface';

1, 2, 4
1, 3, 6
3, 5
5 only
What will be the output of the program?
package foo;  

import java.util.Vector; 

private class MyVector extends Vector 
{ 
    int i = 1;  /* Line 5 */
    public MyVector() 
    {
        i = 2;  
    } 
} 

public class MyNewVector extends MyVector 
{ 
    public MyNewVector() 
    { 
        i = 4; /* Line 15 */
    }
    public static void main(String args[])
    {
        MyVector v = new MyNewVector(); /* Line 19 */
    }
}

Compilation succeeds.
Compilation fails because of an error at line 5.
Compilation fails because of an error at line 15.
Compilation fails because of an error at line 19.
Which is the valid declarations within an interface definition?

public double methoda();
public final double methoda();
static void methoda(double d1);
protected void methoda(double d1);
Which one is a valid declaration of a boolean?

boolean b1 = 0;
boolean b2 = 'false';
boolean b3 = false;
boolean b4 = Boolean.false();
Which three are valid declarations of a float?
  1. float f1 = -343;
  2. float f2 = 3.14;
  3. float f3 = 0x12345;
  4. float f4 = 42e7;
  5. float f5 = 2001.0D;
  6. float f6 = 2.81F;

1, 2, 4
2, 3, 5
1, 3, 6
2, 4, 6
Which is a valid declarations of a String?

String s1 = null;
String s2 = 'null';
String s3 = (String) 'abc';
String s4 = (String) 'ufeed';
What is the numerical range of a char?

-128 to 127
-(215) to (215) - 1
0 to 32767
0 to 65535