Java Core Fundamentals - Practice Test 6
Practice test covering Java language fundamentals including control flow, exceptions, OOP concepts, collections, threading, and core syntax.
Questions
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println(odd);
}
else
{
System.out.println(even);
}
}
Which statement is true?
- Compilation fails.
- odd will always be the output.
- even will always be the output.
- odd will be the output for odd values of x, and even for even values.
import java.awt.;
class Ticker extends Component
{
public static void main (String [] args)
{
Ticker t = new Ticker();
/ Missing Statements ? */
}
}
which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
boolean test = (Component instanceof t);
boolean test = (t instanceof Ticker);
boolean test = t.instanceof(Ticker);
boolean test = (t instanceof Component);
- 1 and 4
- 2 and 3
- 1 and 3
- 2 and 4
What will be the output of the program?
int i = 1, j = 10;
do
{
if(i > j)
{
break;
}
j--;
} while (++i < 5);
System.out.println("i = " + i + " and j = " + j);
- i = 6 and j = 5
- i = 5 and j = 5
- i = 6 and j = 4
- i = 5 and j = 6
What will be the output of the program?
public class Test
{
public static void main(String args[])
{
int i = 1, j = 0;
switch(i)
{
case 2: j += 6;
case 4: j += 1;
default: j += 2;
case 0: j += 4;
}
System.out.println(j = + j);
}
}
- 0
- 2
- 4
- 6
What will be the output of the program?
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;
}
}
- 7 7
- 7 14
- 14 0
- 14 14
Which statement is true?
- catch(X x) can catch subclasses of X where X is a subclass of Exception.
- The Error class is a RuntimeException.
- Any statement that can throw an Error must be enclosed in a try block.
- Any statement that can throw an Exception must be enclosed in a try block.
Which is valid declaration of a float?
- float f = 1F;
- float f = 1.0;
- float f = "1";
- float f = 1.0d;
Which will contain the body of the thread?
- run();
- start();
- stop();
- main();
What will be the output of the program?
import java.util.*;
class H
{
public static void main (String[] args)
{
Object x = new Vector().elements();
System.out.print((x instanceof Enumeration)+",");
System.out.print((x instanceof Iterator)+",");
System.out.print(x instanceof ListIterator);
}
}
- Prints: false,false,false
- Prints: false,false,true
- Prints: false,true,false
- Prints: true,false,false
What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
{
System.out.print( it.next() + " " );
}
- one two three four
- four three two one
- four one three two
- one two three four one
import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw anIOException, which of these is true?
- This program will compile successfully.
- This program fails to compile due to an error at line 4.
- This program fails to compile due to an error at line 6.
- This program fails to compile due to an error at line 13.
Which three guarantee that a thread will leave the running state?
yield()
wait()
notify()
notifyAll()
sleep(1000)
aLiveThread.join()
Thread.killThread()
- 1, 2 and 4
- 2, 5 and 6
- 3, 4 and 7
- 4, 5 and 7
interface DoMath
{
double getArea(int rad);
}
interface MathPlus
{
double getVol(int b, int h);
}
/* Missing Statement(s) ? */
Which code fragment(s) inserted at end of the program, will allow to compile?
- class AllMath extends DoMath { double getArea(int r); }
- interface AllMath implements MathPlus { double getVol(int x, int y); }
- interface AllMath extends DoMath { float getAvg(int h, int l); }
- class AllMath implements MathPlus { double getArea(int rad); }
- abstract class AllMath implements DoMath, MathPlus { public double getArea(int rad) { return rad * rad * 3.14; } }
- 1 only
- 2 only
- 3 and 5
- 1 and 4
class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
which statement, inserted at line 10, creates an instance of Bar?
- Foo.Bar b = new Foo.Bar();
- Foo.Bar b = f.new Bar();
- Bar b = new f.Bar();
- Bar b = f.new Bar();
Which two statements are equivalent?
- 16*4
- 16>>2
- 16/2^2
- 16>>>2
- 1 and 2
- 2 and 4
- 3 and 5
- 1 and 3
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}
- small
- tiny
- huge
- Compilation fails
Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?
- final
- static
- private
- protected
- volatile
What will be the output of the program?
public class BoolTest
{
public static void main(String [] args)
{
int result = 0;
Boolean b1 = new Boolean("TRUE");
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("tRuE");
Boolean b4 = new Boolean("false");
if (b1 == b2) /* Line 10 */
result = 1;
if (b1.equals(b2) ) /* Line 12 */
result = result + 10;
if (b2 == b4) /* Line 14 */
result = result + 100;
if (b2.equals(b4) ) /* Line 16 */
result = result + 1000;
if (b2.equals(b3) ) /* Line 18 */
result = result + 10000;
System.out.println("result = " + result);
}
}
- 0
- 1
- 10
- 10010
Which three statements are true?
- The default constructor initialises method variables.
- The default constructor has the same access as its class.
- The default constructor invokes the no-arg constructor of the superclass.
- If a class lacks a no-arg constructor, the compiler always creates a default constructor.
- The compiler creates a default constructor only when there are no other constructors for the class.
- 1, 2 and 4
- 2, 3 and 5
- 3, 4 and 5
- 1, 2 and 3
Which of the following are Java reserved words?
- run
- import
- default
- implement
- 1 and 2
- 2 and 3
- 3 and 4
- 2 and 4
What will be the output of the program?
public class Test
{
public static void main(String [] args)
{
int I = 1;
do while ( I < 1 )
System.out.print(I is + I);
while ( I > 1 ) ;
}
}
- I is 1
- I is 1 I is 1
- No output is produced
- Compilation error
Which of the following are legal lines of code?
- int w = (int)888.8;
- byte x = (byte)1000L;
- long y = (byte)100;
- byte z = (byte)100L;
- 1 and 2
- 2 and 3
- 3 and 4
- All statements are correct.
What will be the output of the program?
public class Test
{
public static void main(String[] args)
{
int x = 0;
assert (x > 0) ? "assertion failed" : "assertion passed" ;
System.out.println("finished");
}
}
- finished
- Compiliation fails.
- An AssertionError is thrown and finished is output.
- An AssertionError is thrown with the message "assertion failed."
What will be the output of the program?
public class RTExcept
{
public static void throwit ()
{
System.out.print("throwit ");
throw new RuntimeException();
}
public static void main(String [] args)
{
try
{
System.out.print("hello ");
throwit();
}
catch (Exception re )
{
System.out.print("caught ");
}
finally
{
System.out.print("finally ");
}
System.out.println("after ");
}
}
- hello throwit caught
- Compilation fails
- hello throwit RuntimeException caught after
- hello throwit caught finally after
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {}
}
- AC
- BC
- ACD
- ABCD