1. Can two different classes contain methods with the same name? A. No. B. Yes,
ID: 3862134 • Letter: 1
Question
1. Can two different classes contain methods with the same name?
A. No.
B. Yes, but only if the two classes have the same name.
C. Yes, but only if the main program does not create objects of both kinds.
D. Yes, this is always allowed.
2. What is the primary purpose of a constructor?
A. To allow multiple classes to be used in a single program.
B. To copy an actual argument to a method's parameter.
C. To initialize each object as it is declared.
D. To maintain a count of how many objects of a class have been created.
3. Consider this class definition:
public class Quiz
{
private double score;
public int f( )...
public static int g( )...
}
Where could the assignment score=1.0; appear for the private instance variable score?
A. Both f and g can carry out the assignment.
B. f can carry out the assignment, but not g.
C. g can carry out the assignment, but not f.
D. Neither f nor g can carry out the assignment.
4. Suppose we have this method:
public static foo(int[ ] b){
b[0]++; }
What is printed by these statements?
int[ ] x = new int[100];
x[0] = 2;
foo(x);
System.out.println(x[0]);
A. 0
B. 1
C. 2
D. 3
5. Stacks operate as _______________.
A. Last Out, Last Out
B. First In, First Out
C. Last In, First Out
D. None of the above
6. A ______ can be used to reverse the order of a set of data.
Queue
Stack
Software
Heaps
7. I have an array named data, which contains n integers. I want to print all of the numbers, starting at data[0]. BUT if the number 42 occurs, then I want to stop my printing just before the 42 (without printing the 42!) Here is most of my for-loop to accomplish my goal:
for (i = 0; ___________________________________________; i++)
System.out.println(data[i]);
What is the correct way to fill in the blank? (If there is more than one correct answer, please select E.)
A. (data[i] != 42) && (i < n)
B. (data[i] != 42) || (i < n)
C. (i < n) && (data[i] != 42)
D. (i < n) || (data[i] != 42)
E. More than one of the above answers is correct
8. Analyze the following code.
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C
A. count < 100 is always false at Point A
B. count < 100 is always true at Point B
C. count < 100 is always false at Point C
D. count < 100 is always true at Point C
E. count < 100 is always false at Point B
9. To add an element to a stack you use the method.
A. push
B. pop
C. peek
D. size
10. What is y after the following switch statement is executed?
x = 3;
switch (x + 3) {
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
a. 1
b. 4
c. 3
d. 2
e. 0
True/False (4 points each)
11. True/False. An array dynamically grows as needed and essentially has no capacity limitations.
12. True/False. If the algorithm is inefficient, a faster processor will make it very efficient.
13. True/False. A linked list has no set capacity limitations other than the size of the computers memory.
14. True/False. A singly linked list maintains a reference to the first element in the list and then a next reference from each node to the following node in the list.
15. True/False. Postfix notation avoids the need for precedence rules that are required to evaluate infix expressions.
16. True/False An abstract data type is a data type that is not defined within the programming language and must be defined by the programmer.
17. True/False. The new operator returns a reference to a newly created object.
18. True/False. Objects should be encapsulated. The rest of a program should interact with an object only through a well-defined interface.
19. True/False. Instance variables should be declared with public visibility to promote encapsulation.
20. True/False. A constructor cannot have any return type, even void.
21. True/False. A class is the implementation of the blueprint for an object. An object is a specific instance of a class.
22. True/False. Overloaded methods are distinguished from each other by their signatures including the number and type of the parameters.
Fill in the blank (short answer): (4 points each)
23. What are the five basic operations on a stack?
24. What are the 3 primary methods for a stack?___________________________
25. Here is a method with a parameter named spot, which is an integer:
public static foo(int spot)
{
spot += 2;
}
Now, suppose that s is an integer with a value of 40. Then the method foo(s) is activated. What is the value of s after this activation?
26. Convert the following to postfix expression
(9+2) / (6*7 – 2)
Explanation / Answer
1. Can two different classes contain methods with the same name?
It is possible that two or more different class can contain methods with same name. So, the answer is option D: Yes, this is always possible.
2. What is the primary purpose of a constructor?
The purpose of constructor is to instantiate the object of the class. Also, it can be used to set default values to the members of the class. So, the answer is option C: To initialize each object as it is declared.
3. Consider this class definition:
public class Quiz
{
private double score;
public int f( )...
public static int g( )...
}
Where could the assignment score=1.0; appear for the private instance variable score?
Here, score is a non-static private variable. Static methods can only assign values to the static data members where as non-static methods can assign value to non-static members. So, the answer is option B: f can carry out the assignment, but not g.
4. Suppose we have this method:
public static foo(int[ ] b){
b[0]++; }
What is printed by these statements?
int[ ] x = new int[100];
x[0] = 2;
foo(x);
System.out.println(x[0]);
The first element of x is set to value 2 and the array is passed as a parameter to foo method. Arrays are passed by reference which means that any changes done to the array in the foo method will be reflected outside foo method as well. foo methods increment the value of first element of x by 1. So the answer is option D: 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.