oop concepts and lang basics Flashcards

1
Q

Real-world objects contain ___ and ___.

A

Real-world objects contain state and behavior.

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

A software object’s state is stored in ___.

A

A software object’s state is stored in fields.

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

A software object’s behavior is exposed through ___.

A

A software object’s behavior is exposed through methods.

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

Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ___.

A

Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation.

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

A blueprint for a software object is called a ___.

A

A blueprint for a software object is called a class.

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

Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword.

A

Common behavior can be defined in a superclass and inherited into a subclass using theextends keyword.

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

A collection of methods with no implementation is called an ___.

A

A collection of methods with no implementation is called an interface.

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

A namespace that organizes classes and interfaces by functionality is called a ___.

A

A namespace that organizes classes and interfaces by functionality is called a package.

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

The term API stands for ___?

A

The term API stands for Application Programming Interface.

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

The term “instance variable” is another name for ___.

A

The term “instance variable” is another name for non-static field.

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

The term “class variable” is another name for ___.

A

The term “class variable” is another name for static field.

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

A local variable stores temporary state; it is declared inside a ___.

A

A local variable stores temporary state; it is declared inside a method.

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

A variable declared within the opening and closing parenthesis of a method signature is called a ____.

A

A variable declared within the opening and closing parenthesis of a method is called aparameter.

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

What are the eight primitive data types supported by the Java programming language?

A

What are the eight primitive data types supported by the Java programming language?byte, short, int, long, float, double, boolean, char

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

Character strings are represented by the class ___.

A

Character strings are represented by the class java.lang.String.

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

An ___ is a container object that holds a fixed number of values of a single type.

A

An array is a container object that holds a fixed number of values of a single type.

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

Consider the following code snippet.

arrayOfInts[j] > arrayOfInts[j+1]

Which operators does the code contain?

A

Answer: >, +

18
Q

Consider the following code snippet:

int i = 10;
int n = i++%5;

Question: What are the values of i and n after the code is executed?

Question: What are the final values of i and n if instead of using the postfix increment operator (i++), you use the prefix version (++i))?

A

Answer: i is 11, and n is 0.

Answer: i is 11, and n is 1.

19
Q

To invert the value of a boolean, which operator would you use?

A

The logical complement operator “!”.

20
Q

Which operator is used to compare two values, = or == ?

A

The == operator is used for comparison, and = is used for assignment.

21
Q

Explain the following code sample: result = someCondition ? value1 : value2;

A

This code should be read as: “If someCondition is true, assign the value ofvalue1 to result. Otherwise, assign the value of value2 to result.”

22
Q

Change the following program to use compound assignments:

  1. ```
    class ArithmeticDemo { public static void main (String[] args){ int result = 1 + 2; // result is now 3 System.out.println(result); result = result - 1; // result is now 2 System.out.println(result); result = result * 2; // result is now 4 System.out.println(result); result = result / 2; // result is now 2 System.out.println(result); result = result + 8; // result is now 10 result = result % 7; // result is now 3 System.out.println(result); } }
    ~~~
A
class ArithmeticDemo { public static void main (String[] args){ int result = 3; System.out.println(result); result -= 1; // result is now 2 System.out.println(result); result \*= 2; // result is now 4 System.out.println(result); result /= 2; // result is now 2 System.out.println(result); result += 8; // result is now 10 result %= 7; // result is now 3 System.out.println(result); } }
23
Q

In the following program, explain why the value “6” is printed twice in a row:

class PrePostDemo { public static void main(String[] args){ int i = 3; i++; System.out.println(i); // "4" ++i; System.out.println(i); // "5" System.out.println(++i); // "6" System.out.println(i++); // "6" System.out.println(i); // "7" } }
A

The code System.out.println(++i); evaluates to 6, because the prefix version of ++evaluates to the incremented value. The next line, System.out.println(i++);evaluates to the current value (6), then increments by one. So “7” doesn’t get printed until the next line.

24
Q

Operators may be used in building ___, which compute values.

A

Operators may be used in building expressions, which compute values.

25
Q

Expressions are the core components of ___.

A

Expressions are the core components of statements.

26
Q

Statements may be grouped into ___.

A

Statements may be grouped into blocks.

27
Q

The following code snippet is an example of a ___ expression.

1 * 2 * 3

A

The following code snippet is an example of a compound expression.

1 * 2 * 3

28
Q

Statements are roughly equivalent to sentences in natural languages, but instead of ending with a period, a statement ends with a ___.

A

Statements are roughly equivalent to sentences in natural languages, but instead of ending with a period, a statement ends with a semicolon.

29
Q

A block is a group of zero or more statements between balanced ___ and can be used anywhere a single statement is allowed.

A

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.

30
Q

Identify the following kinds of expression statements:

  • aValue = 8933.234;
  • aValue++;
  • System.out.println("Hello World!");
  • Bicycle myBike = new Bicycle();
A

Identify the following kinds of expression statements:

  • aValue = 8933.234; // assignment statement
  • aValue++; // increment statement
  • System.out.println("Hello World!"); // method invocation statement
  • Bicycle myBike = new Bicycle(); // object creation statement
31
Q

The most basic control flow statement supported by the Java programming language is the ___ statement.

A

The most basic control flow statement supported by the Java programming language is theif-then statement.

32
Q

The ___ statement allows for any number of possible execution paths.

A

The switch statement allows for any number of possible execution paths.

33
Q

The ___ statement is similar to the while statement, but evaluates its expression at the ___ of the loop.

A

The do-while statement is similar to the while statement, but evaluates its expression at the bottom of the loop.

34
Q

How do you write an infinite loop using the for statement?

A
for ( ; ; ) { }
35
Q

How do you write an infinite loop using the while statement?

A
while (true) { }
36
Q

Consider the following code snippet.

if (aNumber \>= 0) if (aNumber == 0) System.out.println("first string"); else System.out.println("second string"); System.out.println("third string");
  1. Exercise: What output do you think the code will produce if aNumber is 3?
A
second string third string
37
Q

Write a test program containing the previous code snippet; makeaNumber 3. What is the output of the program? Is it what you predicted? Explain why the output is what it is. In other words, what is the control flow for the code snippet?

A

Solution: NestedIf

second string third string

3 is greater than or equal to 0, so execution progresses to the second ifstatement. The second if statement’s test fails because 3 is not equal to 0. Thus, the else clause executes (since it’s attached to the second if statement). Thus,second string is displayed. The final println is completely outside of any ifstatement, so it always gets executed, and thus third string is always displayed.

38
Q

Using only spaces and line breaks, reformat the code snippet to make the control flow easier to understand.

if (aNumber \>= 0) if (aNumber == 0) System.out.println("first string"); else System.out.println("second string"); System.out.println("third string");
A
if (aNumber \>= 0) if (aNumber == 0) System.out.println("first string"); else System.out.println("second string"); System.out.println("third string");
39
Q

Use braces { and } to further clarify the code and reduce the possibility of errors by future maintainers of the code.

if (aNumber \>= 0) if (aNumber == 0) System.out.println("first string"); else System.out.println("second string"); System.out.println("third string");
A
if (aNumber \>= 0) { if (aNumber == 0) { System.out.println("first string"); } else { System.out.println("second string"); } } System.out.println("third string");
40
Q
A