Object-Oriented Program Design - AP Computer Science A

Card 0 of 374

Question

What does the code print?

class Parent{

final public void show() {

`` System.out.println( "Parent::show() called" );

`` }

}

``

class Child extends Parent {

`` public void show() {

`` System.out.println( "Child::show() called" );

`` }

}

``

public class Main {

`` public static void main(String[] args) {

Parent parent = new Child();

parent .show();

`` }

}

Answer

Final methods can't be overriden. So the code won't compile because of this. Now if the final modifier were to be removed. The code would compile and run and produce:

Child::show()

Compare your answer with the correct one above

Question

Which of the following header statements allow you to omit using the std:: when using cout?

Answer

You must put using namespace std; at the top of your file to avoid having to type std:: every time you use cout and cin.

Compare your answer with the correct one above

Question

Which of the access modifiers don't work in Java?

Answer

One of the benefits to Java is that there is garbage collection. So when data is disposed of its done automatically versus a language like C++ where you need to tell the program to delete dynamically allocated data.

Compare your answer with the correct one above

Question

What line of code is needed to import in order to use Color in the following format Color.Blue, Color.RED, Color.White?

Answer

If you were to use the following format Color.Red, Color.Blue, etc then the correct answer would have to be java.awt.*; While java.awt.Color; does fit the appropriate code, if you import that entire statement, you don't need to put Color.Red just Red and following. The other two import statements are just completely wrong.

Compare your answer with the correct one above

Question

Design a class, SuperHero, that will be used for an existing application. The class should extend the parent class Man. The class should define clothes, skin color, hair color, eye color, good or evil, whether or not powers are had, and a list of personality traits. The class Man supports an implementation of the method doGood. Here is the method stub for doGood:

public String doGood(Boolean good);

Your class will need to support an implementation of doGood.

Choose the best answer.

Answer

This is the correct answer because the class extends the super class Man. It also lists all of the attributes defined in the prompt. In addition to listing all of the attributes, the class also defines an implementation of the doGood method which the prompt states is defined by the parent. One of the answer choices simply defined the stub for the method. The prompt clearly states to support an implementation of the method.

Compare your answer with the correct one above

Question

What does the code print?

class Parent{

final public void show() {

`` System.out.println( "Parent::show() called" );

`` }

}

``

class Child extends Parent {

`` public void show() {

`` System.out.println( "Child::show() called" );

`` }

}

``

public class Main {

`` public static void main(String[] args) {

Parent parent = new Child();

parent .show();

`` }

}

Answer

Final methods can't be overriden. So the code won't compile because of this. Now if the final modifier were to be removed. The code would compile and run and produce:

Child::show()

Compare your answer with the correct one above

Question

Which of the following header statements allow you to omit using the std:: when using cout?

Answer

You must put using namespace std; at the top of your file to avoid having to type std:: every time you use cout and cin.

Compare your answer with the correct one above

Question

Which of the access modifiers don't work in Java?

Answer

One of the benefits to Java is that there is garbage collection. So when data is disposed of its done automatically versus a language like C++ where you need to tell the program to delete dynamically allocated data.

Compare your answer with the correct one above

Question

What line of code is needed to import in order to use Color in the following format Color.Blue, Color.RED, Color.White?

Answer

If you were to use the following format Color.Red, Color.Blue, etc then the correct answer would have to be java.awt.*; While java.awt.Color; does fit the appropriate code, if you import that entire statement, you don't need to put Color.Red just Red and following. The other two import statements are just completely wrong.

Compare your answer with the correct one above

Question

Design a class, SuperHero, that will be used for an existing application. The class should extend the parent class Man. The class should define clothes, skin color, hair color, eye color, good or evil, whether or not powers are had, and a list of personality traits. The class Man supports an implementation of the method doGood. Here is the method stub for doGood:

public String doGood(Boolean good);

Your class will need to support an implementation of doGood.

Choose the best answer.

Answer

This is the correct answer because the class extends the super class Man. It also lists all of the attributes defined in the prompt. In addition to listing all of the attributes, the class also defines an implementation of the doGood method which the prompt states is defined by the parent. One of the answer choices simply defined the stub for the method. The prompt clearly states to support an implementation of the method.

Compare your answer with the correct one above

Question

What's wrong with the following code?

void printsometext()
{
cout<<"printing text
";
for(int i=0;i<3;i=i+1)
{
cout<<i<<"
"
}
return 2;
}

Answer

It is very important when creating a function to select the return type. If you want your function to return an integer, you would place "int" before your function to signify that the function expects an integer output. In this case, the function is declared as void, meaning there is no output. However, the code indicated a return value of 2, which conflicts with the void keyword. To fix this problem, either remove the return statement or change void to int.

Compare your answer with the correct one above

Question

What is the difference between declaring a function as void and declaring it as int?

Answer

Function delcarations are very significant, as you must define what the return type of the function is. A void function doesn't need a return type, whereas the int function must have an integer value as its return type.

Compare your answer with the correct one above

Question

TWO DIMENSIONAL ARRAYS

Given the following initialized array:

int fourth;

int\[\]\[\] myArray = { {1, 2, 3},

{4, 5, 6},

{7, 8, 9} };

Using myArray, how can I store in variable "fourth", the number 4?

Answer

When a two dimensional array is created and initialized, the way to access the items inside the matrix is by calling the array with the row and column (i.e. myArray\[ROW\]\[COLUMN\]). Keeping in mind that arrays start at 0, the number four would be in row 1, column 0. Therefore to save that number into the variable "fourth" we'll do the following:

fourth = myArray\[1\]\[0\];

*Note: myArray\[1\]\[0\] is not the same as myArray\[0\]\[1\].

myArray\[1\]\[0\] =4 because it is the item located at row=1 and column = 0.

myArray\[0\]\[1\] =12 because it is the item located at row=0 and column = 1.

Compare your answer with the correct one above

Question

USING POINTERS

Study the following pseudocode.

**int * var1;**

**int foo = 63;**

**var1 = &foo;**

**var2 = *var1;**

What are the values of var1 and var2?

Answer

Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:

int * var1;

Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.

The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:

var1 = &foo;

Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.

Next in the code, we have the following statement:

**var2 = *var1;**

Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.

Compare your answer with the correct one above

Question

CHOOSING LOOPS

Imagine that I want to write a code for a bank that asks a user whether he/she wants to make a withdrawal, make a deposit, or quit. What type of loop would be best to use in order to make sure the user is able to do as many transactions as they want until they press quit to end the program?

Answer

In our problem, the program has to run AT LEAST ONCE in order to present all of the options to the user. Because of this reason, the best option is a do-while loop because the statements within that loop execute at least one time and the condition to get out of the loop is at the end. In this case, the exit condition would be if the user presses quit (after the options have been shown at least once).

A while loop might work in this case; however it is not necessarily the smartest option. This is because the condition is tested before anything is executed within the loop. This means that whatever is inside the while loop does not necessarily have to be executed at all.

A for loop is also not a good option for this problem because a for loop is executed a set amount of times. In this case, we don't know if the user is going to want to do 1 transaction or 10. Therefore, a for loop is not the best choice.

An infinite loop is also not what we want because it runs forever. In our case, we want the code to stop prompting the user and end after "quit."

Compare your answer with the correct one above

Question

What would be the best data structure for a library? The data is in the form of a title and a number of copies of the title.

Answer

Hash map - key being title and value being the number of copies

Hash maps are a collection of (key, value) pairs.

Hash maps have O(1) access, so this would be the quickest and best way to store the data.

Compare your answer with the correct one above

Question

True or False.

The best data structure to represent a set of keys and values is an array.

Answer

Arrays can be two-dimensional. However, when trying to keep track of keys and values it can become complicated when using an array. HashMaps are the best way to represent data containing keys and values.

Compare your answer with the correct one above

Question

What is not a feature of the Java programming language?

Answer

In Java, the only way to write functions is to make them class methods. Java does have primitive types int, boolean double. There is compile time error checking. Java is an Object Oriented langauge and supports the OO paradigm. There is automatic garbage collection support, which helps manage memory for the user.

Compare your answer with the correct one above

Question

Consider the history of the following popular programming languages:

PHP

Java

Objective-C

Python

Which of the following is the closest ancestor shared by ALL of these languages?

Answer

All of these languages are C-based languages.

  • Ruby was invented in 1995, the same year as PHP, so it could not have influenced earlier languages like Objective-C and Python.
  • Lisp did influence at least one language, Python, but it did not influence any others.
  • Ada directly influenced Java, and because it influenced C, it can be argued that it is an ancestor of these other languages; however, because of this, Ada is not the CLOSEST ancestor.
  • Smalltalk influenced Objective-C, but no other languages on this list.

A clue was the answer "Objective-C," which is a strict superset of C that adds Object Orientation.

Compare your answer with the correct one above

Question

What is the value of the string kitchen after the following code is run?

  1. class home
  2. {
  3. public:
  4. home(string);
  5. void searchhome();
  6. int buyhome();
  7. private:
  8. string kitchen();
  9. };
  10. home::home(string c)
  11. {
  12. kitchen=c;
  13. }
  14. int main()
  15. {
  16. str=’big’;
  17. home(str);
  18. }

Answer

The constructor here in line 4 of the class definition is where it gets tricky. In the initialization of the constructor, we note that the input is a string.

Going down to line 10, to where the constructor function is defined, we see that a constructor with an input of c, which is defined as a string, will set the value of kitchen to c.

Finally, going down to our main code, we see that the value of the constructor in main is 'big', defined in str.

So kitchen='big'.

Compare your answer with the correct one above

Tap the card to reveal the answer