Sample Quiz: Lessons 1 through 8
Part 1: Fill in the Blanks
n An ____________________ is an instance of a class object
n Anything that describes an object is called a (an) ___________________________ attribute
n Anything that an object does is called a ____________________ behavior
n A _______________ can be thought of as a mold, template, or blueprint that the computer
uses to create objects. class
n A value that is passed to an object’s method is called an ___________________ argument.
n OOP stands for _____________________________________ object oriented programming
n gpdraw is called a _______________________ package
n A _______________________ is a method with the same name as the class and no return
type. constructor
n ____________________ is the process of converting a program written in a high-level language
such as Java into the bytecode language the Java interpreter understands. Compiling
n It is useful to think of a class as consisting of nouns called _____________________
and verbs called _____________________ attributes, methods
n A (an) ____________________ variable represents a property of an object. instance
n The primitive data types __________________ and ______________________ are for real
numbers (numbers with decimal places) float double
n The Java escape sequence for new line is ________________________ ‘\n’
n _______________________ are provided by Java to minimize memory use and processing
time data types
n A ______________________ data type contains actual data. primitive
n A ______________________ data type contains a reference. reference
n % is called the ___________________ math operator modulus
n (double) 13/2 is referred to as _________________________ casting
n ___________________ rules govern the order in which a math expression is solved.
Precedence
n ___________________ are used when defining a method and ___________________ are
used when calling a method. Parameters arguments
n The number of parameters, data type of parameters and order of parameters is
referred to as a method’s _______________________ signature
n When 2 or more methods in the same class have the same name they are said to be
________________ overloaded
n _____________________ occurs when a method calls itself to solve another version of the
same problem. Recursion
n ______________________ variables are automatically initialized with a default value (0 for
numbers and false for boolean) instance
n ______________________ variables are initialized with copies of the arguments Parameter
n ________________________ variables are not initialized by default – an initial value must be
supplied Parameter
n _______________________ refers to an area of a program in which an identifier is valid and
has meaning Scope
n The methods in the Math class are static. That means that a (an) ____________________ of
the class does not have to be created in order to use the methods. object
n The java.util package contains utility classes. One of these, the _______________ class, is
used to generate random numbers. Random
n // indicates that the following expression – until the end of the line – is a ________________
comment
n In the expression Scanner in = new Scanner (System.in), Scanner is referred to as a
(an) ______________ , and in is a (an) _______________________ class object
n In Java the variable age is not the same as the variable Age. We therefore say that the
Java language is _____________________________ case sensitive
n 5 + 13/3 + 10 % 3 = ____________ 10 Precedence
n Of Java’s 8 data types, 2 are for real numbers. These are the __________________ and
____________________ float, double
n The boolean data type can have 1 of the following 2 values: _________________ or
____________________ true, false
n Assume that number is a primitive variable. In the expression “number = “ + number, + is
an overloaded operator. In this case it is the _____________________ operator
concatenation
Part 2: True – False
Indicate whether the following statements are true or false by placing a check mark (√) in the appropriate column.
Statement |
True |
False |
A class does not exist when a program executes, except in the form of one or more objects. |
√ |
|
An object must belong to some class and identifiers must begin with a letter. |
√ |
|
A private instance variable has class scope. |
√ |
|
Java provides 5 math operators. operators |
√ |
|
The import statement is used to specify classes or entire packages to be referred to without including their package names in the reference. |
√ |
|
The keyword typecast is used to change the data type of a variable for an operation. |
|
√ |
Errors detected by the interpreter are called compilation errors. |
|
√ |
The comments indicated by /* */ automatically stop at the end of the line. |
|
√ |
The return type in the java main method can be int, double, char, or void |
√ |
|
A class is a template that defines attributes and methods. |
√ |
|
Instance variables are generally declared private. |
√ |
|
Constructors are usually declared public and they can be overloaded. |
√ |
|
The scope of a local variable is the same as the scope of an instance variable. |
|
√ |
An AP Java program must contain a main method |
√ |
|
In the statement import java.util.Scanner; Scanner is a class |
√ |
|
In the statement import javax.swing.*; Access is provided to all classes in the javax package |
|
√ |
Accessor methods typically allow the user to change values of instance variables |
|
√ |
Part 3: Multiple Choice
n Which of the following best explains what is meant by overloading a method?
Note for AP exam: Be carefule when "best" is used. More than one could be correct.
A. Defining another method that does the same thing
B. Defining another method with the same number of parameters
C. Defining another method with the same parameter names
D. Defining another method with the same precondition
E. Defining another method with the same name but different numbers or types of
parameters
The next 2 questions concern the following recursive method:
public int mystery(int k)
{
if (k = = 1) return 0;
else return(1 + mystery(k/2));
}
n What value is returned by the call mystery (16) ?
A. 0
B. 2
D. 5
E. 16
n Which of the following best characterizes the values of k for which the call mystery (k)
leads to an infinite recursion?
A. No values
B. All positive values //16 does not
C. All nonpositive values
D. All odd values //this is integer division
E. All even values //this is integer division
n Which of the following is not an example of a good use of comments?
Note for AP exam. Be careful with "not" statements.
A. Comments included at the beginning of a method to specify the method's pre and
postconditions
B. Comments included at the end of every line of a method to explain what that line of
code does
C. Comments included at the beginning of a method to say which of the class' s fields
are modified by that method
D. Comments included in a class's constructor to explain how the class object is
initialized
E. Comments included before a loop to say what is true each time the loop is executed
n Assume that a class includes the following three methods:
public static int min(int x, int y)
{
if (x < y) return X;
else return Y;
}
public static int min(String s, String t)
{
if (s.length() < t.length(return s.length( );
else return t.length();
}
public static void testMin()
{
System.out.println(min(3, "hello"));
}
Which of the following best describes what happens when this code is compiled and
executed?
A. The code will not compile because the types of the arguments used in the call to min
do not match the types of the parameters in either version of min.
B. The code will not compile because it includes two methods with the same name and
the same return type.
C. The code will not compile because it includes two methods with the same name and
the same number of parameters.
D. The code will compile and execute without error; the output will be 3 .
E. The code will compile and execute without error; the output will be 5.