test_java question

advertisement
EXERCISES
1. The command to compile a class in the file Test.java is
A. java Test
B. java Test.java
C. javac Test.java
E. JAVAC Test.java
D. javac Test
2. The Java __________ runs a Java program.
A. compiler
B. interpreter
C. program
D. translator
3. Which of the following is a correct signature for the main method?
A. static void main(String[] args[])
B. public static void main(String[] args)
C. public void main(String[] args)
D. public static void main(Strings[] args)
5. If you attempt to add an int, a byte, a long, and a double, the result will be a
__________ value.
A. byte
B. int;
C. long;
D. double;
6. If a program compiles fine, but it terminates abnormally at runtime, then the program
suffers __________.
A. a compilation error
C. a logical error
7. Is the following loop correct? for (; ; );
B. a runtim error
d. None of the above
A. Yes
B. No
8. Which of the following modifiers must be present for a method inside the main class to
be called inside the main method?
A. public
C. static
9. Analyze the following code:
public class Test
{
public static void main (String args[])
{
int i = 0;
for (i=0; i<10; i++);
System.out.println(i+4);
}
}
B. private
D. None of the above is necessary.
A. The program has a syntax error because of the semicolon (;) on the for loop line.
B. The program compiles despite the semicolon (;) on the for loop line, and displays 4.
C. The program compiles despite the semicolon (;) on the for loop line, and displays 14.
D. None of the above.
10. An object is an instance of a __________.
A. program
B. class
C. method
D. data
11. To restrict access of a data member or a method to the class itself:
A. Use the private modifier.
B. You cannot use the private modifier with the static modifier.
C. Use the static modifier.
D. None of the above.
12. You can declare two variables with the same name in __________.
A. a method
C. two nested blocks in a method
13. What is the output of the following code?
public class Test
{
public static void main(String[] args)
{
String s1 = new String("Welcome to Java!");
String s2 = s1;
B. a block
D. different methods in a class
if (s1 = = s2)
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is not equal to s2");
}
}
A. s1 is equal to s2
14. What is the output of the following code?
public class Test
{
public static void main(String[] args)
{
String s1 = new String("Welcome to Java!");
String s2 = new String("Welcome to Java!");
if (s1 = = s2)
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is not equal to s2");
}
}
B. s1 is not equal to s2
A. s1 is equal to s2
B. s1 is not equal to s2
15. Which of the following class definitions defines a legal abstract class?
A.
class A
{ abstract void unfinished(){ } }
B.
class A
{ abstract void unfinished(); }
C.
abstract class A
{ abstract void unfinished(); }
D.
public class abstract A
{ abstract void unfinished(); }
16. What modifier should you use so that a class in a different package cannot access the
class, but its subclasses in any package can access it?
A. public
C. protected
18. Which of the following statements is valid?
B. private
D. Use the default modifier.
A. int i = new int(30);
B. double d[] = new double[30];
C. int i[] = (3, 4, 3, 2);
D. char[] c = new char();
19. When you pass an array to a method, the method receives __________.
A. a copy of the array
B. a copy of the first element
C. the reference of the array
D. none of the above
20. Analyze the following code:
public class Test
{
public static void main(String[] args)
{
int[] x = {1, 2, 3, 4, 5};
xmethod(x, 5);
}
public static void xmethod(int[] x, int length)
{
System.out.print(" "+x[length-1]);
xmethod(x, length-1);
}
}
A. The program displays 1 2 3 4 5.
B. The program displays 1 2 3 4 5 and then raises a runtime exception.
C. The program displays 5 4 3 2 1.
D. The program displays 5 4 3 2 1 and then raises a runtime exception.
21. What is displayed on the console when running the following program?
class Test
{
public static void main (String[] args)
{
try
{
System.out.println("Welcome to Java");
}
finally
{
System.out.println("The finally clause is executed");
}
}
}
A. Welcome to Java
B. Welcome to Java followed by The finally clause is executed in the next line
C. The finally clause is executed
D. None of the above
32. What is displayed on the console when running the following program?
class Test
{
public static void main(String[] args)
{
try
{
System.out.println("Welcome to Java");
int i = 0;
int y = 2/i;
System.out.println("Welcome to HTML");
}
finally
{
System.out.println("The finally clause is executed");
}
}
}
A. Welcome to Java.
B. Welcome to Java followed by The finally clause is executed in the next line.
C. The program displays three lines: Welcome to Java, Welcome to HTML, The finally
clause is executed.
D. None of the above.
33. Choose all the correct answers
a. A byte can store the numbers is between –127 to 128
b. An int can store the numbers is between –32768 to 32767
c. A default value of char type is null
d. All of them are true
9. To declare a two dimension array, choose the correct statemenst: (choose 2)
a. int a[][];
b. int a[2][3];
c. int []a[];
c. int a;
10. What happen with the following code?
class A
{
public static void main(String []a)
{
int i;
System.out.print(i);
i = 10;
System.out.print(i);
}
}
a. It will print 010 to the screen
b. It will not compile because there is an error
c. It will compile but doesn’t execute
d. None of the above
11. Java is an _______ programming language
a. interpreted
b. compiled
c. oriented
d. All of them
13. Guess the output of the following code
class A
{
public static void main(String a[])
{
int i = -24;
System.out.print( i >> 3);
System.out.prin( i );
}
}
a. 3 3
b. -3 –24
c. –8 -24
d. –8 –8
14. Choose the legal identifiers
a. 1sum
b. sum.one
c. $sumone
d. sum one
16. _____ implies that the method doesn’t have code and it has to be implemented in the
subclass
a. public
b. static
c. abstract
d. Another modifier
17. What will the output of the following program
class A
{
int i = 2;
public static void main(String a[])
{
System.out.print( i++ );
System.out.print( ++i );
}
}
a. 34
b. 23
c. 24
21. What will the output of the following program
class A
{
public static void main(String a[])
{
System.out.print( 27 ^ 2 );
}
}
a. 729
b. 25
c. 26
d. 24
d. None of the above
Download