Programming Concepts Practice Test 1 1) Which of the following is a

advertisement
Programming Concepts Practice Test 1
1) Which of the following is a constant, according to Java naming conventions?
a.
PI
b.
Test
c.
x
d.
radius
2) Consider the following statement:
System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\t2night");
This statement will output ________ lines of text
a) 1
b) 2
c) 3
d) 4
e) 5
3) Which character below is not allowed in an identifier?
a) $
b) _
c) 0 (zero)
d) q
e) ^
4) Which of the following cannot cause a syntax error to be reported by the Java compiler?
a) Mismatched {}
b) Missing */ in a comment that begins with /*
c) Missing ;
d) Extra blank lines.
5) Which of the following statements will print a single line containing "hello there"?
a) System.out.println( "hello" );
System.out.println( " there" );
b) System.out.println( "hello" , " there" );
c) System.out.println( "hello" );
System.out.print( " there" );
d) System.out.print( "hello" );
System.out.println( " there" );
6) Assume that x, y, and z are all ints equal to 50, 20, and 6 respectively. What is the result of x / y /
z?
a. 0
b. 12
c. 16
d. A syntax error as this is syntactically invalid
e. A run-time error because this is a division by 0
7) What will be the value of z as a result of executing the following code?
int x = 5, y = 28;
double z;
z = (double) y / x;
a.
b.
c.
d.
5.6
5
3.0
5.0
8) What is output with the statement System.out.println(x+y); if x and y are int values where x=10
and y=5?
a. 15
b. 105
c. 10 5
d. x+y
e. An error since neither x nor y is a String
9) What is output with the statement System.out.println(""+x+y); if x and y are int values where
x=10 and y=5?
a. 15
b. 105
c. 10 5
d. x+y
e. An error since neither x nor y is a String
10) Suppose x is 3. What is x after x *= 2?
a.
b.
c.
d.
e.
4
2
6
3
8
11) The expression (int)(76.0252175 * 100) / 100.0 evaluates to _________.
a. 76
b. 76.0252175
c. 76.03
d. 76.02
e. 76.0
12) Which of the following is not a syntax error?
a. System.out.println( 'Hello world!' ):
b. System.out.println( "Hello
world!" );
c. System.out.println( "Hello" +
"world!" );
d. System.out.println( Hello world! );
13) If you want to output the text "hi there", including the quote marks, which of the following could
do that?
a) System.out.println("hi there");
b) System.out.println(""hi there"");
c) System.out.println("\"hi there");
d) System.out.println("\"hi there\"");
e) none, it is not possible to output a quote mark because it is used to mark the beginning and ending of
the String to be output
14) If you want to store into the String name the value "George Bush", you would do which
statement?
a) String name = "George Bush";
b) String name = new String("George Bush");
c) String name = "George" + " " + "Bush";
d) Any of the above would work
15) Suppose that String name = "Frank Zappa". What will the instruction name.substring(2, 6)
return?
a) "ran"
b) "rank"
c) "rank "
d) "ank Z"
e) "rank Zappa"
f) "rk"
g) "ank "
h) "ank"
Free –Form Questions
16) What is wrong with the following class definition? Fix all the syntax errors.
Public Class Program
\\ A problem program
import java.util.Scanner;
Public static voided main[Strings( ) args]
{
system.out.println('This program has syntax errors');
\* oh, my... *\
system.out.println('Input a number');
\* lots of errors *\
x = scan.nextInt( );
square = Math.pow(x);
system.out.println('The square of the number is x');
}
17) The following program takes as inputs from the user (1) the cost per kilowatt-hour in cents as an
integer and (2) the number of kilowatt-hours the appliance used in a year as an integer. Complete
the program so that it computes and displays the energy cost of the appliance in dollars in a year.
You do not need to format the output.
import java.util.Scanner;
public class EnergyCost{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
//Read in cost per kilowatt-hour and number of hours in a year
System.out.println("Enter the cost per kilowatt-hour in cents for the applicance:");
int unitCost = scan.nextInt();
System.out.println("Enter the number of kilowatt-hours in a year:");
int numOfHours = scan.nextInt();
//compute the energy cost
//display output
}
}
18) The following program prompts the user to enter the number of minutes (e.g. 5 million). Complete
the program so it computes and displays the number of years and days for the minutes. For
simplicity, assume a year has 365 days. Here is a sample run:
Enter the number of minutes: 5000000
5000000 minutes is approximately 9 years and 187 days
import java.util.Scanner;
public class MinutesToDays{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number of minutes:");
int minutes = sc.nextInt();
//computes the years and days
//display results
}
}
19) The following program reads a number between 1,000 and 999,999 from the user, where the user
enters a comma in the input. Complete the program so it prints the number without a comma. Here
is a sample input and output:
Please enter an integer between 1,000 and 999,999: 34,953
Output: 34953
import java.util.Scanner;
public class NumberInput{
public static void main(String[] args)
{
// Read in a number as a string between 1,000 and 999,999
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number between 1,000 and 999,999");
String number = scan.next();
//compute the output
//display the output
}
}
Download