oop concepts and lang basics Flashcards
Real-world objects contain ___ and ___.
Real-world objects contain state and behavior.
A software object’s state is stored in ___.
A software object’s state is stored in fields.
A software object’s behavior is exposed through ___.
A software object’s behavior is exposed through methods.
Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ___.
Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation.
A blueprint for a software object is called a ___.
A blueprint for a software object is called a class.
Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword.
Common behavior can be defined in a superclass and inherited into a subclass using theextends keyword.
A collection of methods with no implementation is called an ___.
A collection of methods with no implementation is called an interface.
A namespace that organizes classes and interfaces by functionality is called a ___.
A namespace that organizes classes and interfaces by functionality is called a package.
The term API stands for ___?
The term API stands for Application Programming Interface.
The term “instance variable” is another name for ___.
The term “instance variable” is another name for non-static field.
The term “class variable” is another name for ___.
The term “class variable” is another name for static field.
A local variable stores temporary state; it is declared inside a ___.
A local variable stores temporary state; it is declared inside a method.
A variable declared within the opening and closing parenthesis of a method signature is called a ____.
A variable declared within the opening and closing parenthesis of a method is called aparameter.
What are the eight primitive data types supported by the Java programming language?
What are the eight primitive data types supported by the Java programming language?byte, short, int, long, float, double, boolean, char
Character strings are represented by the class ___.
Character strings are represented by the class java.lang.String.
An ___ is a container object that holds a fixed number of values of a single type.
An array is a container object that holds a fixed number of values of a single type.
Consider the following code snippet.
arrayOfInts[j] > arrayOfInts[j+1]
Which operators does the code contain?
Answer: >, +
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))?
Answer: i is 11, and n is 0.
Answer: i is 11, and n is 1.
To invert the value of a boolean, which operator would you use?
The logical complement operator “!”.
Which operator is used to compare two values, = or == ?
The == operator is used for comparison, and = is used for assignment.
Explain the following code sample: result = someCondition ? value1 : value2;
This code should be read as: “If someCondition is true, assign the value ofvalue1 to result. Otherwise, assign the value of value2 to result.”
Change the following program to use compound assignments:
- ```
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); } }
~~~
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); } }
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" } }
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.
Operators may be used in building ___, which compute values.
Operators may be used in building expressions, which compute values.