Java Language Fundamental - General Knowledge Question and Answer

Java Language Fundamental - Question and Answer

Java is a _________ language.  


weakly typed
strogly typed
moderate typed
None of these

Answer:

strogly typed

Share Me:

Which is a valid keyword in java?  


interface
string
Float
unsigned

Answer:

interface is a valid keyword.

Option B is wrong because although "String" is a class type in Java, "string" is not a keyword.

Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float.

Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java.


Share Me:

Which is a reserved word in the Java programming language?  


method
native
subclasses
reference

Answer:

The word "native" is a valid keyword, used to modify a method declaration.

Option A, D and E are not keywords. Option C is wrong because the keyword for subclassing in Java is extends, not 'subclasses'.


Share Me:

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};

Answer:

The only legal array declaration and assignment statement is Option D

Option A is wrong because it initializes an int array with String literals.

Option B is wrong because it use something other than curly braces for the initialization.

Option C is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array.


Share Me:

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

Answer:

All the words in option B are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is not used and has no function.

Option A is wrong because the keyword for the primitive int starts with a lowercase i.

Option C is wrong because "virtual" is a keyword in C++, but not Java.

Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final.

Option E is wrong because "include" is a keyword in C, but not in Java.


Share Me:

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

Answer:

(1), (3), (4), (5) are the correct statements.

(2) is wrong because the default value for a String (and any other object reference) is null, with no quotes.

(6) is wrong because the default value for boolean elements is false.


Share Me:

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.

Answer:

(1), (2), and (4) are legal array declarations. With an array declaration, you can place the brackets to the right or left of the identifier. Option A looks strange, but it's perfectly legal to split the brackets in a multidimensional array, and place them on both sides of the identifier. Although coding this way would only annoy your fellow programmers, for the exam, you need to know it's legal.

(3) and (5) are wrong because you can't declare an array with a size. The size is only needed when the array is actually instantiated (and the JVM needs to know how much space to allocate for the array, based on the type of array and the size).


Share Me:

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

Answer:

(1), (2) and (3) are correct. Interfaces can have constants, which are always implicitly publicstatic, and final. Interface constant declarations of publicstatic, and final are optional in any combination.


Share Me:

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;

Answer:

Option B is the legal way to declare and initialize an array with five elements.

Option A is wrong because it shows an example of instantiating a class named Array, passing the integer value 5 to the object's constructor. If you don't see the brackets, you can be certain there is no actual array object! In other words, an Array object (instance of class Array) is not the same as an array object.

Option C is wrong because it shows a legal array declaration, but with no initialization.

Option D is wrong (and will not compile) because it declares an array with a size. Arrays must never be given a size when declared.


Share Me:

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

Answer:

(1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the integer value 27128, which is legal because it fits into an unsigned 16-bit integer. char c3 = 0xbeef; is a hexadecimal representation of the integer value 48879, which fits into an unsigned 16-bit integer. char c6 = '\uface'; is a Unicode representation of a character.

char c2 = 'face'; is wrong because you can't put more than one character in a char literal. The only other acceptable char literal that can go between single quotes is a Unicode value, and Unicode literals must always start with a '\u'.

char c4 = \u0022; is wrong because the single quotes are missing.

char c5 = '\iface'; is wrong because it appears to be a Unicode representation (notice the backslash), but starts with '\i' rather than '\u'.


Share Me:

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);

Answer:

Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract.

Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A finalmethod cannot be abstract.

Option C is wrong. static is concerned with the class and not an instance.

Option D is wrong. protected is not permitted when declaring a method of an interface. See information below.

Member declarations in an interface disallow the use of some declaration modifiers; you cannot use transientvolatile, or synchronized in a member declaration in an interface. Also, you may not use the private and protected specifiers when declaring members of an interface.


Share Me:

Which one is a valid declaration of a boolean?  


boolean b1 = 0;
boolean b2 = 'false';
boolean b3 = false;
boolean b4 = Boolean.false();

Answer:

A boolean can only be assigned the literal true or false.

Share Me:

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

Answer:

(1) and (3) are integer literals (32 bits), and integers can be legally assigned to floats (also 32 bits). (6) is correct because (F) is appended to the literal, declaring it as a float rather than a double (the default for floating point literals).

(2), (4),and (5) are all doubles.


Share Me:

Which is a valid declarations of a String?  


String s1 = null;
String s2 = 'null';
String s3 = (String) 'abc';
String s4 = (String) '\ufeed';

Answer:

Option A sets the String reference to null.

Option B is wrong because null cannot be in single quotes.

Option C is wrong because there are multiple characters between the single quotes ('abc').

Option D is wrong because you can't cast a char (primitive) to a String (object).


Share Me:

What is the numerical range of a char?  


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

Answer:

char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values.


Share Me:

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]);
        }
    }
}
What will be the output of the program? public class CommandArgsThree { publ
 


0 0
1 2
0 0 0
1 2 3

Answer:

In argCopy[0] = args;, the reference variable argCopy[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements.


Share Me:

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 > java CommandArgs 1 2 3 4
 


args[2] = 2
args[2] = 3
args[2] = null
An exception is thrown at runtime.

Answer:

An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException.


Share Me:

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: > java F0091 world
 


Hello
Hello Foo91
Hello world
The code does not run.

Answer:

Option D is correct. A runtime error will occur owning to the main method of the code fragment not being declared static:

Exception in thread "main" java.lang.NoSuchMethodError: main

The Java Language Specification clearly states: "The main method must be declared publicstatic, and void. It must accept a single argument that is an array of strings."


Share Me:

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

Answer:

The second dimension of the array referenced by theDogs has not been initialized. Attempting to access an uninitialized object element (System.out.println(theDogs[2][0].toString());) raises a NullPointerException.


Share Me:

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.

Answer:

The word "signed" is not a valid modifier keyword in the Java language. All number primitives in Java are signed. Hence the Compilation will fails.


Share Me:

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

and the command-line invocation is

java CommandArgsTwo 1 2 3

 


0 1 2
1 2 3
0 0 0
An exception is thrown at runtime

Answer:

An exception is thrown because at some point in (System.out.print(" " + argh[y]);), the value of x will be equal to y, resulting in an attempt to access an index out of bounds for the array. Remember that you can access only as far as length - 1, so loop logical tests should use x < someArray.length as opposed to x < = someArray.length.


Share Me:

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].leng   


7
9
11
13

Answer:

The loops use the array sizes (length).

It produces 11 lines of output as given below.

 

D:\Java>javac Test.java

D:\Java>java Test
size = 1
size = 2
size = 3
size = 4
size = 2
size = 3
size = 3
size = 4
size = 5
size = 6
size = 7

 

Therefore, 11 is the answer.


Share Me:

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

java X a b

 


names
null
Compilation fails
An exception is thrown at runtime

Answer:

The names array is initialized with five null elements. Then elements 0 and 1 are assigned the String values "a" and "b" respectively (the command-line arguments passed to main). Elements of names array 2, 3, and 4 remain unassigned, so they have a value of null.


Share Me:

Size of int in Java is  


16 bit
32 bit
64 bit
Depends on execution environment

Answer:

32 bit

Share Me:

The implicit return type of a constructor is  


void
A class object in which it is defined.
There is no return type.
None of the above

Answer:

A class object in which it is defined.

Share Me: