Comp 1510Sample Midterm Questions
MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
1) For a computer to communicate over the Internet, it must use
A) The combined TCP/IP protocol
B) The TCP protocol
C) The Ethernet protocol
D) The IP protocol
E) The ARPANET protocol
2) The main method for a Java program is defined by
A) public static main( )
B) public static main(String[ ] args);
C) public static main(String[ ] args)
D) private static main(String[ ] args)
E) the main method could be defined as in A, C or D but not B
3) Mistyping "println" as "printn" will result in
A) converting the statement into a comment
B) a run-time error
C) a syntax error
D) no error at all
E) a logical error
4) A unique aspect of Java that allows code compiled on one machine to be executed on a machine of a different
hardware platform is Java's
A) syntax
B) use of objects
C) use of exception handling
D) bytecodes
E) all of the above
5) The ability to directly obtain a stored item by referencing its address is known as
A) random access
B) sequential access
C) volatility
D) read-only access
E) fetch access
6) Which character below is not allowed in an identifier?
A) 0 (zero) B) $ C) ^ D) q E) _
Use the following class definition to answer the questions below.
public class Questions1_4
{
public static void main(String[ ] args)
{
System.out.print("Here");
System.out.println("There " + "Everywhere");
System.out.println("But not" + "in Texas");
}
}
7) A reasonable comment for this program might be
A) // a program that demonstrates the differences between print, println and how + works
B) // a program that demonstrates nothing at all
C) // a program that outputs a message about Texas
D) // a program that outputs the message "Here There Everywhere But not in Texas"
E) // a program that contains three output statements
1
8) How many lines of output are provided by this program?
A) 1 B) 2 C) 3 D) 4 E) 5
For the following questions, refer to the class defined below:
import java.util.Scanner;
public class Questions33_34
{
public static void main(String[ ] args)
{
int x, y, z;
double average;
Scanner scan = Scanner.create(System.in);
System.out.println("Enter an integer value");
x = scan.nextInt( );
System.out.println("Enter another integer value");
y = scan.nextInt( );
System.out.println("Enter a third integer value");
z = scan.nextInt( );
average = (x + y + z) / 3;
System.out.println("The result of my calculation is " + average);
}
}
9) Question33_34 computes
A) The correct average of x, y and z as a double
B) the remainder of the sum of x, y and z divided by 3
C) The correct average of x, y and z as an int
D) The average of x, y, and z as a double, but the result may not be accurate
E) the sum of x, y and z
10) Which library package would you import to use NumberFormat and DecimalFormat?
A) java.lang B) java.util C) java.text D) java.io E) java.beans
11) The advantages of the DecimalFormat class compared with the NumberFormat class include
A) precise control over the number of digits to be displayed
B) control over the presence of a leading zero
C) the ability to display a % automatically at the beginning of the display
D) the ability to truncate values rather than to round them
E) only A and B
12) In the StringMutation program shown in Listing 3.1, if phrase is initialized to "Einstein" what will Mutation #3 yield if
Mutation #1: mutation1 = phrase.concat(".")?
A) EINSTEIN. B) einstein. C) XINSTXIN. D) Einstein. E) xinstxin.
13) An API is
A) an Application Programming Interface
B) an Abstract Programmer's Interface
C) an Application Programmer's Interface
D) an Absolute Programming Interface
E) an Abstract Programming Interface
14) A JPanel can be added to a JFrame to form a GUI. In order to place other GUI components such as JLabels and
Buttons into the JFrame, which of the following messages is passed to the JPanel?
A) getContentPane
B) insert
C) get
D) add
E) include
2
15) StringTokenizer is a class in the java.util library that can divide a String based on some delimiter String (a delimiter
is a separator). If the instruction StringTokener st = new StringTokenizer(str, "&&"); is executed, where str is some
String, then st divides up str into separate Strings whenever
A) two ampersands are found
B) a single ampersand ("&") is found
C) two blank spaces are found
D) a blank space is found
E) two ampersands or the substring "and" or "AND" is found
16) An example of passing message to a String where the message has a String parameter would occur in which of the
following messages?
A) substring
B) toUpperCase
C) length
D) equals
E) none of the above, it is not possible to pass a String as a parameter in a message to a String
17) A variable whose scope is restricted to the method where it was declared is known as a(n)
A) private instance data
B) parameter
C) global variable
D) public instance data
E) local variable
18) How many times will the following loop iterate?
int x = 10;
do {
System.out.println(x);
x--;
} while (x > 0);
A) 10 times B) 1 time C) 11 times D) 0 times E) 9 times
19) If a switch statement is written that contains no break statements whatsoever,
A) each of the case clauses will be executed every time the switch statement is encountered
B) this is equivalent to having the switch statement always take the default clause, if one is present
C) this is not an error, but nothing within the switch statement ever will be executed
D) this is a syntax error and an appropriate error message will be generated
E) none of the above
20) Volatility is a property of
A) ROM
B) disk
C) RAM
D) computer networks
E) software
21) Which of the following is a legal Java identifier?
A) i
B) class
C) ilikeclass!
D) idon'tlikeclass
E) i-like-class
22) Which library package would you import to use the class Random?
A) java.util B) java.io C) java.lang D) java.beans E) java.text
3
23) Autoboxing is
A) the automatic conversion of a wrapper object to/from its corresponding primitive type
B) the automatic enclosure of a graphics object within a bounding box
C) the automatic conversion of an enumeration into its numeric representation
D) the automatic widening of ints into floats or doubles, as required
E) none of the above
24) Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls
is legal?
A) doublefoo(0);
B) doublefoo(0.555);
C) doublefoo(0.1 + 0.2);
D) doublefoo(0.1, 0.2);
E) all of the above are legal except for D
Given the nested if-else structure below, answer questions below.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
25) If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
A) 0 B) 2 C) 3 D) 4 E) 5
26) Binary numbers are composed entirely of
A) 0s and 1s
B) 0s, 1s and 2s
C) 1s
D) any digits between 0 and 9
E) 0s
27) The Random class has a method nextFloat( ) which returns a random float value between
A) -2,147,483,648 and +2,147,483,647
B) 0 and 1
C) -1 and +1
D) 0 and 99
E) 1 and 100
28) Consider the following enumeration
enum Speed { FAST, MEDIUM, SLOW };
A) The ordinal value of SLOW is 1
B) The ordinal value of MEDIUM is 2
C) The name of the Speed enumeration whose ordinal value is zero is FAST
D) The name of the Speed enumeration whose ordinal value is one is SLOW
E) None of the above
4
For the questions below, use the following class definition.
import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;
public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}
public String toString( )
{
DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced
return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours
}
}
29) Which of the following patterns should be used in place of "xxxx" when instantiating df so that the gpa to be output in
typical form (like 3.810)?
A) "#.0" B) "#.###" C) "0.# " D) "0.0##" E) "0.000"
30) Given that s is a String, what does the following loop do?
for (int j = s.length( ); j > 0; j--)
System.out.print(s.charAt(j-1));
A) it prints s out backwards after skipping the last character
B) it prints s out backwards
C) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
D) it prints s out forwards
E) it prints s out backwards but does not print the 0 th character
TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false.
31) Objects are defined by a class that describes the characteristics common to all instances of the class.
32) All information is stored in the computer using binary numbers. True
33) You cannot cast a String to be a char and you cannot cast a String which stores a number to be an int, float or
double.
True
34) When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
true
35) A GUI container is an object that defines a screen element.
true
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".
36) The expression (!done && x <= y) is true. True
37) In a Java program, dividing by 0 is a syntax error. false
38) Java is able to represent 255 * 255 * 255 = 16,581,375 distinct colors. true
5
39) The Random class' setSeed( ) method allows you to restart the pseudo-random number sequence. true
40) A constructor may contain a return statement so long as no value (or expression) is returned.
false
ESSAY. Write your answer in the space provided or on a separate sheet of paper.
41) What is the output of the following when the main method is executed?
public class Question4
{
public static void main(String[ ] args)
{
System.out.println("hi there");
System.out.println(" ");
System.out.println("how are you doing today? ");
}
}
42) What is wrong with the following assignment statement? Assume x and y are both String objects.
String z = x.equals(y);
For the questions below: Assume an interactive Java program which asks the user for their first name and last name, and outputs
the user's initials.
43) Write a statement using a Scanner method to get the first name interactively.
For the questions below, write the requested portions of a class called BaseballPlayer. This class contains the following data
instances:
private String name;
private String position;
private int numAtBats;
private int numSingles;
private int numDoubles;
private int numTriples;
private int numHomeRuns;
private double battingAverage;
44) Write the constructor, which is passed the player's name and position.
45) A data verification loop is a loop that asks the user to input a value within a restricted range repeatedly until the user
has input a value within the requested range. Write a data verification loop that that inputs an int value x within the
range 0 and 100. Assume cs1.Keyboard has been imported.
46) Rewrite the following comment so that it can appear over multiple lines.
// This is one really enormously long comment that might run off the page
47) Provide three examples of code using assignment statements where one assignment statement would result in a
syntax error, one would result in a logical error, and one would result in a run-time error.
For the questions below: Assume an interactive Java program which asks the user for their first name and last name, and outputs
the user's initials.
48) Write a method to extract the initial from the first name.