Академический Документы
Профессиональный Документы
Культура Документы
For the multiple choice problems, select the best answer for each one and select the appropriate letter on
your answer sheet.
Be careful - more than one answer may seem to be correct. Many questions are tricky.
Some problems ask you to determine whether something is valid. Something is valid if it would not generate a compiler error and would execute without the program crashing.
I. True/False: (2 points each)
T F
T F
2. In Eclipse it is possible to step through a program one line at a time, however to do this you
must enclose the code in the program inside of special comment markers.
T F
3. If you want to use Eclipse to develop your Java programs, your compute must also have a
Java Development Kit (JDK) installed.
T F
4. Java programs can be compiled and run from the command line.
T F
5. The main() method in a Java program must always call a mainLoop() method, otherwise the
program cannot run.
T F
6. The name of a Java class must begin with a capital letter, otherwise the program will not run.
T F
7. Variable names in Java by convention use mixed case, where the words are concatenated
together and the first letter of each word is capitalized.
T F
8. In a Java program a method that does not return anything can either have a return type of void
or have no return type listed at all.
T F
9. Java methods are private by default, which means they cannot be called from outside of that
file.
T F
T F
T F
T F
T F
14. Single line comments using // can be nested inside block comments using /* */
T F
15. A single System.out.println( ...) statement can cause 3 output lines to be displayed.
T F
16. Several System.out.print( ...) statements can cause a single line of output to be displayed.
T F
T F
18. Variables that are of type boolean can be used interchangeably with variables of type int
T F
19. Variables that are of type int can be stored into double variables, but double variables cannot
be conversely stored into an int variable.
T F
20. float and double variables are exactly the same. Those two variable declarations can be used
interchangeably.
T F
21. To end a Java program both the break or the exit statement may be used.
T F
22. A do loop (also called a do-while loop) is the best choice for displaying a menu and handling
the user response.
T F
23. The section of code shown below would compile and run and give as output:
int sum = 0;
for( int i=0; i<=3; i++)
sum += i;
System.out.println( sum);
T F
24. The section of code shown below would compile and run:
int sum = 0;
for( ; ; )
;
System.out.println( sum);
T F
25. Any code that can be written with a switch-case statement could also be written with multiple
if-else statements.
T F
26. The following statements in Java would be useful for helping check if the length of a String
is 3 characters long.
String name = "123";
if( name.length() = 3) {
System.out.println("Length is three.");
}
T F
T F
10 Done
int y = 4;
int z = 6;
System.out.print( "" + z + y);
System.out.println(" Done");
T F
1 Done
char c=a;
switch (c){
case a: System.out.print("1");
case b: System.out.print("2");
case c: System.out.print("3");
}
System.out.println(" Done");
T F
30. The following Java program could have all the spaces and new-line characters removed so
that it was all on one line, and it would still compile, run, and give the same output.
public class Sample {
public static void main(String[] args)
{
int x = 5;
int y = x+2;
System.out.println("x+y:" + x + y);
}
}
a)
b)
c)
d)
e)
358
458
459
369
None of the above
32. Assume the code segment shown below, where method swapValues2 is called.
Output of this segment of code is:
// ... other code
int x = 3, y = 7;
System.out.println( change( x, y) );
// ... other code
a)
b)
c)
d)
e)
73
37
10
Does not compile
None of the above
a)
b)
c)
d)
e)
3*9
3+9
39
93
None of the above
34. Consider the program segment given below. Its output is:
int value = 0;
for( int i=0; i<7; i++) {
value += i%2;
}
System.out.println("Value is: " + value);
a)
b)
c)
d)
e)
Value is: 3
Value is: 7
Value is: 10
16
20
24
20
25
30
24
30
36
what are the values for variables start, end, first and last?
for( int i=start; i<=end; i++) {
for( int j=first; j<last; j++) {
System.out.printf("%5d",i*j);
}
System.out.println();
}
a)
b)
c)
d)
e)
int start=7,
int start=4,
int start=7,
int start=3,
None of the above
end=3,
end=7,
end=4,
end=7,
first=6,
first=3,
first=6,
first=4,
last=4;
last=6;
last=3;
last=6;
36. What is the output of the code given in the two columns below when an object of type Confuse is created and used to call method startUp()?
class Confuse
{
private int x;
private int y;
public Confuse()
{
x = 3; y = 6;
}
a)
b)
c)
d)
e)
7
6
12
11
None of the above
x+y
x*x
x*y
xy
None of the above
method38( 1324));
a)
b)
c)
d)
e)
4
1
10
Compiler error
None of the above
39. Consider ClassA given below, along with the driver class ClassADriver for it.
class ClassA
{
private int x;
public ClassA()
{
x = 1;
}
public ClassA( int x)
{
this.x = x+1;
}
class ClassADriver
{
public void doIt()
{
int value = 7;
ClassA instance1 = new ClassA( value);
instance1.x = value - 1;
instance1.changeValue( 2);
System.out.println("value is: " +
instance1.x);
}
}//end ClassADriver
When running method doIt() in the ClassADriver class, the output will be:
a)
b)
c)
d)
e)
value is: 5
value is: 6
value is: 7
doesnt compile
None of the above
40. Consider the method shown below. When passed a String, what does it return?
public String method40( String theWord)
{
char c;
String newWord = "";
for( int i=0; i<theWord.length(); i++) {
c = theWord.charAt( i);
newWord = "";
for( int j=0; j<theWord.length(); j++) {
if( (j<=i)) {
newWord = newWord + theWord.charAt( j);
}
else if( theWord.charAt( j) == c) {
newWord = newWord + theWord.charAt( j);
}
}
theWord = newWord;
}
return newWord;
}
a)
b)
c)
d)
e)
Every other letter of the original String, starting with the first
Every other letter of the original String, starting with the second
The original String with all duplicate letters eliminated
Only the letters from the original String that had duplicates
None of the above