Pretest for final exam Flashcards

1
Q

T/F: In a class, fields can be any data type.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

T/F: In a class, fields can only be primitive data types.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

T/F: In a class, fields can only be user-defined data types.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

T/F: In Java, a field is a variable inside a class but outside the methods of that class.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

T/F: static variables declared in a class are also called class variables.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

T/F: You create an object from a class blueprint.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

T/F: In a method call, there must be exactly the same number of arguments in parentheses as there are formal parameters in the method header.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

T/F: Inside a Java instance method, you can use the this keyword as a name for the calling object.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

T/F: An instance variable that is declared private can be accessed by name outside of the class definition.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

T/F: Mutator methods usually return void, but can return a boolean value indicating whether the change of the instance variable was a success.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

T/F: Only the default constructor has access to the this parameter.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

T/F: A main method can be placed inside any class definition

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

T/F: Primitive types are reference types.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T/F: Every class has a toString method and an equals method inherited from the Object class.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

T/F: In a class, a field that is not declared static is called an instance variable.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

T/F: static variables (or fields) of a class are shared by all instances of the class.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

T/F: static variables (or fields) of a class can be accessed through the class name as well as an instance reference.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

T/F: A static variable (or field) of a class is associated with the class itself rather than with any object of the class.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

T/F: The new operator returns a reference to the object that was created.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

T/F: When a program is running, the garbage collector automatically cleans up the objects
to which the program holds no more references.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

T/F: You can use a static field to keep track of how many objects have been created from a particular class.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

T/F: Any object can manipulate a class variable, but class variables can also be manipulated without creating an object of the class.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

T/F: An instance method of a class can call a static method of the same class.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

T/F: A constructor of a class can be declared private .

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

T/F: The Java compiler always adds a default constructor to a user defined class.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

T/F: Each instantiated object will have its own copy of a class variable.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

T/F: When an object is passed to a method, a copy of each of the object’s data members are created and passed to the method.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

T/F: A class can have multiple constructors.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

T/F: A formal parameter is a variable declared in the method header.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

T/F: To use a predefined method you must know the code in the body of the method.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

T/F: The word final is a return type in Java.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

T/F: void methods never have parameters.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

T/F: If a formal parameter is a variable of a primitive data type, then after copying the value of the actual argument, there is no connection between the formal parameter and the
actual argument.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

T/F: You can use the class String to pass strings as parameters to a method and change the actual parameter.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

T/F: Within a class, any instance method can call any other method, without exception.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

T/F: A variable declared within a block is accessible only within the block from the point at
which it is declared until the end of the block.

A

True

37
Q

T/F: No two methods in the same class can have the same name.

A

False

38
Q

T/F: A local variable is a variable declared within a method or block that can be accessed only by code within that same method or block.

A

True

39
Q

T/F: The value returned by a value-returning method can be saved for further calculation in the program.

A

True

40
Q

T/F: The return statement must be the last line of the method.

A

False

41
Q

T/F: Methods other than the main method only execute when called.

A

True

42
Q

T/F: Members of a class are usually classified into three categories: static, public, and void.

A

False

43
Q

T/F: If a member of a class is public you cannot access it outside the class.

A

False

44
Q

What can you say about the name of a class?

A. It must start with an uppercase letter.
B. The convention is to start with an uppercase letter.

A

B

45
Q

What can you say about the name of constructors?

A. They must be the same name as the class name.
B. They can be any name, just like other methods.
A

A

46
Q
What is a constructors return type?
A. void
B. Object
C. the class name
D. The constructor does not have a return type
A

D

47
Q

T/F: It is legal to have more than one constructor in a class

A

True

48
Q

In a typical class, what is the recommendation for access modifiers?

A. instance variables are private and methods are private
B. instance variables are private and methods are public
C. instance variables are public and methods are private
D. instance variables are public and methods are public

A

B

49
Q

In a class, if a field is private

A. it can be accessed directly from any class.
B. it can be accessed directly only from inside its class.

A

B

50
Q

Accessors and mutators are

A. instance variables of a class.
B. used to access and modify field variables of a class from outside the class.
C. constructor methods.
A

B

51
Q

Accessor (getter) methods typically take
A. no parameters.
B. one parameter, of the same type as the corresponding field.

A

A

52
Q

Mutator (setter) methods typically take
A. no parameters.
B. one parameter, of the same type as the corresponding field.

A

B

53
Q

Accessor (getter) methods typically
A. are void methods.
B. return the same type as the corresponding field.

A

B

54
Q
Although mutator (setter) methods can return any type, they typically return
A. void .
B. the same type as the corresponding field.
A

A

55
Q

To enable method chaining, mutator methods
A. return a reference to this object.
B. return the same type as the corresponding field.

A

A

56
Q

When coding a method of a class that performs calculations on fields of that class,

A. these fields must be passed as parameters to the method.
B. these fields do not need to be passed as parameters to the method because the method has direct access to them.

A

B

57
Q
What is the keyword used to declare a class variable or method?
A. static
B. final 
C . class
A

A

58
Q
Information is passed to a method in: 
A. the method name.
B. that method’s return.
C. the called method.
D. the formal parameters to the method.
A

D: The formal parameter to the method

59
Q

Which of the following statements describes block scope?

A. It begins at the opening { of the class declaration and terminates at the closing }
B. It limits label scope to only the method in which it is declared.
C. It begins at the identifier’s declaration and ends at the terminating right brace
} of the block in which the identifier is declared.
D. It is valid for one statement only.

A

C. It begins at the identifier’s declaration and ends at the terminating right brace } of the block in which the identifier is declared.

60
Q

In a class containing methods with the same name, the methods are distinguished by:

A. Number of formal parameters.
B. Types of formal parameters.
C. Order of formal parameters.
D. Return type.
E. A and B.
F. A,B and C.
G. A,B,C and D.
A

F

61
Q

An overloaded method is one that
A. has a different name as another method, but the same formal parameters.
B. has the same name as another method, but different formal parameters.
C. has the same name and formal parameters as a method defined in another class.
D. has the same name and formal parameters, but a different return type as another method.

A

B. has the same name as another method, but different formal parameters.

62
Q
Which of the following methods should be used to compare two strings lexicographically (i.e., dictionary order)?
A. equals
B. equalsIgnoreCase
C. compareTo
D. ==
A

C:
For Example:

the call ”Hello”.compareTo(”Hi”) returns a negative value indicating that the String ”Hello” comes before the String ”Hi” in dictionary order;

the call ”Hi”.compareTo(”Hello”) returns a positive value indicating that the String ”Hi” comes after the String ”Hello” in dictionary order;

the call ”HiHello”.compareTo(”HiHello”) returns zero, indicating that the two strings are equal.

63
Q
The body of a method that returns a value must contain at least one
A. void statement.
B. break statement.
C. throws statement.
D. return statement.
A

D

64
Q
A variable whose existence is confined to a method definition is called
A. an instance variable.
B. a local variable.
C. a global variable.
D. none of the above
A

B

65
Q
The parameter this refers to
A. instance variables
B. local variables
C. global variables
D. the calling object
A

D

66
Q
Two methods that are expected to be in all Java classes are
A. getName and setName
B. toString and equals
C. compareTo and charAt
D. toLowerCase and toUpperCase
A

B

67
Q
To hide details of a class implementation, you mark the class’s fields as
   A. B. C. D.
public protected
private
all of the above
A

C

68
Q
The signature of a method is defined by its name as well as the number and types of its
A. formal parameters
B. actual arguments
C. return values
D. local variables
A

A

69
Q
The method trim of the String class trims off 
A. Leading white space
B. Trailing white space
C. Both leading and trailing white space
D. Blank spaces
A

C

70
Q
A class that contains public methods that can change the data in the objects of the class is called a/an:
A. mutable class
B. immutable class
C. invariant class
D. none of the above
A

A

71
Q

To copy an object to another object in the same class
A. Use the assignment statement, e.g. object1 = object2;
B. Write a copy method that will make a field by field assignment of the two objects.
C. Write a method to do a byte-by-byte assignment of the two objects.
D. Since objects consist of several fields, you cannot copy them.

A

B

72
Q
The following Fun class has two methods fun1 and fun2 and an instance variable num. Which of the methods should ideally be declared static ?
       public class Fun
       {
           private int num; // an instance field
           public int fun1(int num)
           {
               return num * num;
           }
           public int fun2()
           {
return num; }
}

A. fun1
B. fun2
C. both
D. none

A

A,
because fun1 does not use any instance members of the class. fun1’s formal parameter num hides the instance variable num; all references to num in fun1 is to
its formal parameter num, which local to fun1.

On the other hand, all references to num in fun2 are to the instance variable num, which can be used only by instance methods of the class (and not by its static methods).

73
Q

A static method of a class is one that can be called with _____

A. an instance variable
B. a local variable
C. a global variable
D. both the class name as well as the objects of that class
A

D

74
Q

Analyze the following code and choose the best answer:

class Circle {
         private double radius;
         public Circle(double radius) {
           radius = radius;
} }

A. The program has a compilation error because it does not have a main method.
B. The program will compile, but you cannot create an object of Circle with a
specified radius. The object will always have radius 0.
C. The program has a compilation error because you cannot assign radius to
radius .
D. The program does not compile because Circle does not have a default con-
structor.

A

B

75
Q
Which of these is true of the relationship between objects and classes? 
A. A class is an instance of an object.
B. An object is an instance of a class.
C. An object is inside a class.
D. A class is inside an object.
A

B

76
Q

What is the range of the random number r generated by the code below?

Random rand = new Random(); int r = rand.nextInt(8) + 2;

A. 3≤r≤10
B. 3≤r≤9
C. 2≤r≤10
D. 2≤r≤9

A

D. Since the value returned from the call rand.nextInt(8) is between 0 and 7, 76) inclusive, the value of rand.nextInt(8)+2 is between 2 and 9, inclusive; that is,
2 ≤ r ≤ 9.

77
Q

If a local variable of a method of a class has the same name as an instance variable (or field) of that class, which variable is used when the method is executing?

A. the local variable
B. the class variable
C. the instance variable
D. None of the above since this would cause a compiler error.
A

A

78
Q

What is an actual argument?
A. The value passed into a method by a method call
B. The value returned by a method
C. The type of the method
D. A variable declared in the method header

A

A

79
Q

Given the following method header, which of the following does NOT demonstrate method overloading?

int mystery(int x, int y)

A. int mystery(int x, int y, int z)
B. int mysterious(int x, int y)
C. int mystery(char x)
D. double mystery(double x, double y)

A

B

80
Q

Given the following method header, which of the following would be an incorrect demonstration of method overloading?

int mystery(int a, int b)

A. int mystery(int x, int y)
B. double mystery(double x, double y)
C. int mystery(int x, int y, int z)
D. char mystery(char x, char y, char z)

A

A

81
Q

Which of the following is NOT an actual argument?

A. Expressions used in a method call
B. Variables used in a method call
C. Variables defined in a method heading
D. Constant values used in a method call

A

C

82
Q

Which modifier is used to specify that a method cannot be used outside a class?

A. public
B. static
C. abstract
D. private

A

D

83
Q

How can a method send a primitive value back to the caller?

A. It cannot send primitive values back to the caller
B. By using the return statement
C. By assigning the value to one of its parameters
D. It can call its caller with the value

A

B

84
Q

Consider the following method:

public static int mystery(int x, int y){ return x < y ? x : y; }

What is the name of the method?
A. public
B. static
C. int
D. mystery
E. none of the above
A

D

85
Q

Consider the following method:

public static int mystery(int x, int y){ return x < y ? x : y; }

What does the method do?
A. it returns parameter x or y, whichever is smaller.
B. it returns parameter x or y, whichever is bigger.
C. it returns expression x < y
D. it returns expression ”x : y”

A

A

86
Q

Consider the following method:

public static int mystery(int x, int y){ return x < y ? x : y; }

What would be the output of the following statement?

int s = mystery(2, mystery(1, 3));

A. 1
B. 2
C. 3
D. There would be no output; this is not a valid statement

A

A

87
Q

Consider the following method:

public static int mystery(int x, int y){ return x < y ? x : y; }

 What is the return type of the method? 
A. public
B. void
C. int
D. mystery
E. Nothing is returned.
A

C

88
Q

Consider the following method:

public static int mystery(int x, int y){ return x < y ? x : y; }

Which of the following is a valid call to the method given in question 84?

A. mystery(5, 4)
B. mystery(int x, int y);
C. mystery(int 5, int 4)
D. public static int mystery(5, 4);

A

A