Java - Test 1


Професиональные тесты - Java - Оценка уровня
1. Каким будет результат выполнения следующей программы?
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));
}
}








2. Каким будет результат выполнения следующей программы?
class SC2 
{

public static
void main(String [] args)
{

SC2 s = new SC2();
s.start();
}


void
start()
{

int
a = 3;
int
b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}


String foo()
{

return
"foo";
}
}






3. Каким будет результат выполнения следующей программы?
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);
}
}






4. Какие два выражения эквивалентны?






5.
public void foo( boolean a, boolean b)
{

if
( a )
{

System.out.println("A"); /* Line 5 */
}

else if
(a && b) /* Line 7 */
{

System.out.println( "A && B");
}

else
/* Line 11 */
{

if
( !b )
{

System.out.println( "notB") ;
}

else

{

System.out.println( "ELSE" ) ;
}
}
}


Какое из утверждений верно для этого кода:






6. Каким будет результат выполнения следующей программы?
Float f = new Float("10"); 
switch
(f)
{

case
10: System.out.println("Ten");
case
0: System.out.println("Zero");
default
: System.out.println("Default");
}






7. Каким будет результат выполнения следующей программы?
public class Test 
{

public static
void aMethod() throws Exception
{

try
/* Line 5 */
{

throw
new Exception(); /* Line 7 */
}

finally /* Line 9 */
{

System.out.print("finally "); /* Line 11 */
}
}

public static
void main(String args[])
{

try

{

aMethod();
}

catch
(Exception e) /* Line 20 */
{

System.out.print("exception ");
}

System.out.print("finished"); /* Line 24 */
}
}






8. Какое из следующих утверждений верно для класса: java.util.ArrayList?





9. Какое из следующих утверждений верно для method-local inner class?





10. Какой из перечисленных ниже кодов подходит для загрузки thread?
class X implements Runnable 
{

public static
void main(String args[])
{

/* Missing code? */

}

public
void run() {}
}







11. Каким будет результат выполнения следующей программы?
class MyThread extends Thread 
{

public static
void main(String [] args)
{

MyThread t = new MyThread();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}

public
void run()
{

System.out.print("Thread ");
}
}






12. Каким будет результат выполнения следующей программы?
class MyThread extends Thread 
{

MyThread() {}
MyThread(Runnable r) {super(r); }
public
void run()
{

System.out.print("Inside Thread ");
}
}

class
MyRunnable implements Runnable
{

public
void run()
{

System.out.print(" Inside Runnable");
}
}

class
Test
{

public static
void main(String[] args)
{

new
MyThread().start();
new
MyThread(new MyRunnable()).start();
}
}






13. Каким будет результат выполнения следующей программы?
class s implements Runnable 
{

int
x, y;
public
void run()
{

for
(int i = 0; i < 1000; i++)
synchronized(this)
{

x = 12;
y = 12;
}

System.out.print(x + " " + y + " ");
}

public static
void main(String args[])
{

s run = new s();
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t1.start();
t2.start();
}
}






14.
public class Test 
{

public
void foo()
{

assert false; /* Line 5 */
assert false; /* Line 6 */
}

public
void bar()
{

while
(true)
{

assert false; /* Line 12 */
}

assert false; /* Line 14 */
}
}

Код какой линии помешает составлению программы?






15. Каким будет результат выполнения следующей программы?
public class Test 
{

public static
int y;
public static
void foo(int x)
{

System.out.print("foo ");
y = x;
}

public static
int bar(int z)
{

System.out.print("bar ");
return
y = z;
}

public static
void main(String [] args )
{

int
t = 0;
assert t > 0 : bar(7);
assert t > 1 : foo(8); /* Line 18 */
System.out.println("done ");
}
}






16. Какое из следующих утверждений верно?





17.
public class Test2 
{

public static
int x;
public static
int foo(int y)
{

return
y * 2;
}

public static
void main(String [] args)
{

int
z = 5;
assert z > 0; /* Line 11 */
assert z > 2: foo(z); /* Line 12 */
if
( z < 7 )
assert z > 4; /* Line 14 */

switch
(z)
{

case
4: System.out.println("4 ");
case
5: System.out.println("5 ");
default
: assert z < 10;
}


if
( z < 10 )
assert z > 4: z++; /* Line 22 */
System.out.println(z);
}
}


На какой линии из кода неправильно употреблен assert?






18. Каким будет результат выполнения следующей программы?
public class NFE 
{

public static
void main(String [] args)
{

String s = "42";
try

{

s = s.concat(".5"); /* Line 8 */
double
d = Double.parseDouble(s);
s = Double.toString(d);
int
x = (int) Math.ceil(Double.valueOf(s).doubleValue());
System.out.println(x);
}

catch
(NumberFormatException e)
{

System.out.println("bad number");
}
}
}






19. Каким будет результат выполнения следующей программы?
public class Test138 
{

public static
void stringReplace (String text)
{

text = text.replace ('j' , 'c'); /* Line 5 */
}

public static
void bufferReplace (StringBuffer text)
{

text = text.append ("c"); /* Line 9 */
}

public static
void main (String args[])
{

String textString = new String ("java");
StringBuffer textBuffer = new StringBuffer ("java"); /* Line 14 */
stringReplace(textString);
bufferReplace(textBuffer);
System.out.println (textString + textBuffer);
}
}






20. Каким будет результат выполнения следующей программы? (от jdk1.6 вверх)?
public class BoolTest 
{

public static
void main(String [] args)
{

Boolean b1 = new Boolean("false");
boolean b2;
b2 = b1.booleanValue();
if
(!b2)
{

b2 = true;
System.out.print("x ");
}

if
(b1 & b2) /* Line 13 */
{

System.out.print("y ");
}

System.out.println("z");
}
}