OOP (Object Oriented Programming) Final Review
1.5 Q1: ________ models software in terms similar to those that people use to describe real-world objects. Object-oriented programming Object-oriented design Procedural programming None of the above
b. Object-oriented design.
2.8 Q2: Which of the following is not a compilation error? Neglecting to initialize a local variable in a method before it is used. Placing a semicolon at the end of the first line of an if statement. Omitting the left and right parenthesis for the condition of an if statement. All are compilation errors.
b. Placing a semicolon at the end of the first line of an if statement.
3.6 Q1: A default constructor has how many parameters? 0. 1. 2. a variable number.
a. 0.
4.13 Q2: What is the result value of c at the end of the following code segment? int c = 8; c++; ++c; c %= 5; a. 0. b. 1. c. 3. d. None of the above.
a. 0.
2.7 Q1: What is the value of result after the following Java statements execute (assume all variables are of type int)? a = 4; b = 12; c = 37; d = 51; result = d % a * c + a % b + a; 119 51 127 59
a. 119
3.3 Q1: What is the name of the values the method call passes to the method for the parameters? Arguments. References. Objects. Values.
a. Arguments.
4.9 Q1: Counter-controlled repetition is also known as: a. Definite repetition b. Indefinite repetition c. Multiple-repetition structure d. Double-repetition structure
a. Definite repetition
1.5.4 Q1: Which of the following statements is false? a. Each class can be used only once to build many objects. b. Reuse helps you build more reliable and effective systems, because existing classes and components often have undergone extensive testing, debugging and performance tuning. c. Just as the notion of interchangeable parts was crucial to the Industrial Revolution, reusable classes are crucial to the software revolution that has been spurred by object technology. d. Avoid reinventing the wheel—use existing high-quality pieces wherever possible. This software reuse is a key benefit of object-oriented programming.
a. Each class can be used only once to build many objects Actually, you can reuse a class many times to build many objects.
4.14 Q2: Which of the following is not a primitive type? a. char b. float c. String d. int
c. String
4.11 Q2: Which of the following statements is true? a. A while statement cannot be nested inside another while statement. b. An if statement cannot be nested inside another if statement. c. A while statement cannot be nested inside an if statement. d. None of the above is true.
None of the above is true. D
3.5 Q1: Which of the following statements is false? A floating-point number is a number with a decimal point. Java provides two primitive types for storing floating-point numbers in memory—float and double. Variables of type float represent single-precision floating-point numbers and have seven significant digits. Variables of type double represent double-precision floating-point numbers; these require twice as much memory as float variables and provide 14 significant digits.
Variables of type double represent double-precision floating-point numbers; these require twice as much memory as float variables and provide 14 significant digits. Actually, variables of type double provide 15 significant digits.
3.2.1 Q1: Which of the following statements is false? Each class declaration that begins with the access modifier private must be stored in a file that has the same name as the class and ends with the .java filename extension. Every class declaration contains keyword class followed immediately by the class's name. Class, method and variable names are identifiers. An object has attributes that are implemented as instance variables and carried with it throughout its lifetime.
a. Each class declaration that begins with the access modifier private must be stored in a file that has the same name as the class and ends with the .java filename extension. private should be public.
2.3 Q1: Which is the output of the following statements? System.out.print( "Hello "); System.out.println( "World" ); Hello World HelloWorld Hello World World Hello
a. Hello World
4.9 Q3: Where can local variables declared within a method's body be used? a. Only in that method between the line in which they were declared and the closing brace of that method. b. Same as (a), but not within while or if statements. c. Only within while or if statements within the method in which they were declared. d. Anywhere within the class.
a. Only in that method between the line in which they were declared and the closing brace of that method.
4.5 Q1: What is output by the following Java code segment? int temp = 200; if (temp > 90) System.out.println("This porridge is too hot."); if (temp < 70) System.out.println("This porridge is too cold."); if (temp == 80) System.out.println("This porridge is just right!"); a. This porridge is too hot. b. This porridge is too cold. c. This porridge is just right! d. None of the above.
a. This porridge is too hot.
2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using Two forward slashes ( // ). Three forward slashes ( /// ). A slash and a star ( /* ). A slash and two stars ( /** ).
a. Two forward slashes ( // ).
2.8 Q1: What will be output after the following Java statements have been executed (assume all variables are of type int)? a = 4; b = 12; c = 37; d = 51; if ( a < b ) System.out.println( "a < b" ); if ( a > b ) System.out.println( "a > b" ); if ( d <= c ) System.out.println( "d <= c" ); if ( c != d ) System.out.println( "c != d" ); a < b c != d a < b d <= c c != d a > b c != d a < b c < d a != b
a. a < b c != d
3.4 Q1: Java requires a ________ call for every object that's created. constructor destructor parameterless parameterized
a. constructor.
2.6 Q1: Which of the following statements does not alter the value stored in a memory location? int a; number = 12; y = y + 2; width = Integer.parseInt(input);
a. int a;
2.5.3 Q1: Which of the following is a variable declaration statement? int total; import java.util.Scanner; public static void main( String args[] ) // first string entered by user
a. int total;
int i = 0; while (i < 4) { readData(); i = i + 1; } Explicitly and Implicitly Converting Between Primitive Types 4.10 Q5: In an expression containing values of the types int and double, the ________ values are ________ to ________ values for use in the expression. a. int, promoted, double. b. int, demoted, double. c. double, promoted, int. d. double, demoted, int.
a. int, promoted, double.
5.2 Q2: The control variable of a counter-controlled loop should be declared as ________to prevent errors. int. float. double. Any of the above.
a. int.
2.2 Q2: Which of the following is not a valid Java identifier? my Value $_AAA1 width m_x
a. my Value (Identifiers may not contain blanks).
3.3 Q2: Which of the following is a Scanner method? nextLine. nextText. nextWord. readNext.
a. nextLine.
2.8 Q3: Each of the following is a relational or equality operator except: <= =! == >
b. =!
Examples Using the for Statement 5.4 Q1: Which of the following for-loop headers results in equivalent numbers of iterations: for (int q = 1; q <= 100; q++) for (int q = 100; q >= 0; q--) for (int q = 99; q > 0; q -= 9) for (int q = 990; q > 0; q -= 90) A and B. C and D. A and B have equivalent iterations and C and D have equivalent iterations. None of the loops have equivalent iterations.
b. C and D.
3.3 Q3: Multiple parameters are separated with what symbol? Dot separator Comma. Parentheses. Braces.
b. Comma.
3.4.1 Q1: Which of the following statements is true? Constructors can specify parameters and return types. Constructors can specify parameters but not return types. Constructors cannot specify parameters but can specify return types. Constructors can specify neither parameters nor return types.
b. Constructors can specify parameters but not return types.
4.12 Q2: What does the expression x %= 10 do? a. Adds 10 to the value of x, and stores the result in x. b. Divides x by 10 and stores the remainder in x. c. Divides x by 10 and stores the integer result in x. d. None of the above.
b. Divides x by 10 and stores the remainder in x.
3.2.2 Q3: Which of the following statements is true? Local variables are automatically initialized. Every instance variable has a default initial value—a value provided by Java when you do not specify the instance variable's initial value. The default value for an instance variable of type String is void. The argument types in the method call must be identical to the types of the corresponding parameters in the method's declaration.
b. Every instance variable has a default initial value—a value provided by Java when you do not specify the instance variable's initial value.
1.8 Q1: Java was originally developed for: Operating systems development. Intelligent consumer devices. Personal computers. Distributed computing.
b. Intelligent consumer devices.
Section 4.14 Primitive Types 4.14 Q1: Java is considered a strongly typed language because: a. The primitive types in Java are portable across all computer platforms that support Java. b. Java requires all variables to have a type before they can be used in a program. c. Instance variables of the primitive types are automatically assigned a default value. d. All of the above.
b. Java requires all variables to have a type before they can be used in a program
BOOKMARK 4.8 Q1: What is output by the following Java code segment? int temp = 180; while (temp != 80) { if (temp > 90) { System.out.print("This porridge is too hot! "); // cool down temp = temp - (temp > 150 ? 100 : 20); } else { if (temp < 70) { System.out.print( "This porridge is too cold! "); // warm up temp = temp + (temp < 50 ? 30 : 20); } } } if (temp == 80) System.out.println("This porridge is just right!"); a. This porridge is too cold! This porridge is just right! b. This porridge is too hot! This porridge is just right! c. This porridge is just right! d. None of the above.
b. This porridge is too hot! This porridge is just right!
3.2.2 Q2: Which of the following statements is false? Scanner method next reads characters until any white-space character is encountered, then returns the characters as a String. To call a method of an object, follow the object name with a comma, the method name and a set of parentheses containing the method's arguments. A class instance creation expression begins with keyword new and creates a new object. A constructor is similar to a method but is called implicitly by the new operator to initialize an object's instance variables at the time the object is created.
b. To call a method of an object, follow the object name with a comma, the method name and a set of parentheses containing the method's arguments. To call a method of an object, follow the object name with a dot separator (.), the method name and a set of parentheses containing the method's arguments.
2.3 Q2: Which of the following is the escape character? * \ \n "
b. \
2.5.1 Q1: All import declarations must be placed inside the class declaration's body. before the class declaration. after the class declaration. all of the above will work.
b. before the class declaration.
3.5 Q2: Floating-point literals are of type ________ by default. float double real decimal
b. double.
Section 4.11 Formulating Algorithms: Nested Control Statements 4.11 Q1: Local variables must be ________. a. initialized when they're declared. b. initialized before their values are used in an expression. c. declared and initialized in two steps. d. declared at the top of the method's body.
b. initialized before their values are used in an expression.
Application: Summing the Even Integers from 2 to 20 5.4 Q3: Which of the following is equivalent to this code segment? int total = 0; for (int i = 0; i <= 20; i += 2) total += i; int total = 0; for (int i = 20; i < 0; i += 1) total += i; int total = 0; for (int i = 0; i <= 20; total += i, i += 2); int total = 0; for (int i = 0, i <= 20, total += i; i += 2); int total = 0; for (int i = 2; i < 20; total += i, i += 2);
b. int total = 0; for (int i = 0; i <= 20; total += i, i += 2)
4.4 Q6: Which of the following is not a Java keyword? a. do b. next c. while d. for
b. next
3.2.6 Q1: Declaring instance variables ________ is known as data hiding or information hiding. secure private static masked
b. private.
3.3 Q1: Types in Java are divided into two categories. The primitive types are boolean, byte, char, short, int, long, float and double. All other types are ________ types. static reference declared source
b. reference
2.7 Q2: Which of the following is not an arithmetic operator? + - . %
c. .
4.14 Q4: What is the size in bits of an int? a. 8 b. 16 c. 32 d. 64
c. 32
2.5.6 Q2: Given the Java statement number1 = input.nextInt(); in which number1 is an int and input is a Scanner, which of the following occurs if the user does not enter a valid int value? A compilation error occurs. The program continues executing and assigns the value 0 to number1. A runtime logic error occurs. None of the above.
c. A runtime logic error occurs.
2.5.2 Q1: The filename for the public class that begins with public class Addition must be public.java. public.class.java. Addition.java. addition.java.
c. Addition.java.
5.3 Q1: Consider the following two Java code segments: Segment 1 Segment 2 int i = 0; for (int i = 0; i <= 20; i++) while (i < 20) { { System.out.println(i); i++; } System.out.println(i); } Which of the following statements are true? The output from these segments is not the same. The scope of the control variable i is different for the two segments. Both (a) and (b) are true. Neither (a) nor (b) is true.
c. Both (a) and (b) are true.
4.10 Q6: Which of the following statements is false? a. To ensure that the operands in a mixed-type expression are of the same type, Java performs implicit conversion on selected operands. b. Cast operators are unary operators. c. Cast operators associate from right to left and are one level lower in precedence than the multiplicative operators. d. Cast operators are formed by placing parentheses around the name of a type.
c. Cast operators associate from right to left and are one level lower in precedence than the multiplicative operators.
3.4 Q1: Attributes of a class are also known as: Constructors. Local variables. Fields. Classes.
c. Fields.
1.5.7 Q1: Which of the following statements is false? a. Classes (and their objects) encapsulate, i.e., encase, their attributes and methods. b. A class's (and its object's) attributes and methods are intimately related. c. For objects to communicate effectively with one another, each must know how the other object is implemented. d. Information hiding is crucial to good software engineering.
c. For objects to communicate effectively with one another, each must know how the other object is implemented. Actually, Objects are normally not allowed to know how other objects are implemented—implementation details are hidden within the objects themselves.
2.5.3 Q2: A(n) ________ enables a program to read data from the user. printf. import declaration. Scanner. main.
c. Scanner.
4.6 Q5: Which of the following statements is true? a. Both syntax errors and logic errors are caught by the compiler. b. Both syntax errors and logic errors have effects at execution time. c. Syntax errors are caught by the compiler. Logic errors have effects at execution time. d. Logic errors are caught by the compiler. Syntax errors have effects at execution time.
c. Syntax errors are caught by the compiler. Logic errors have their effects at execution time.
2.2 Q4: Which of the following does not contain a syntax error? System.out.println( 'Hello world!' ): System.out.println( "Hello world!" ); System.out.println( "Hello world!" ); System.out.println( Hello world! );
c. System.out.println( "Hello world!" );
4.2 Q1: Which of the following is not an algorithm? a. A recipe. b. Operating instructions. c. Textbook index. d. Shampoo instructions (lather, rinse, repeat).
c. Textbook index. A textbook index contains useful information, but does not specify actions and the order in which they are to be performed.
4.8 Q2: Which of the following is not an error (either a syntax error or a logic error)? a. Neglecting to include an action in the body of a while statement that will eventually cause the condition to become false. b. Spelling a keyword (such as while or if) with a capitalized first letter. c. Using a condition for a while statement that is initially false. d. An infinite loop.
c. Using a condition for a while statement that is initially false.
3.2.1 Q3: Which of the following statements is true? Each object (instance) of the class shares the class's instance variables. Most instance-variable declarations are preceded with the keyword public, which is an access modifier. Variables or methods declared with access modifier private are accessible only to methods of the class in which they're declared. None of the above is true.
c. Variables or methods declared with access modifier private are accessible only to methods of the class in which they're declared.
4.12 Q1: Which of the following code segments does not increment val by 3: a. val += 3; b. val = val + 1; val = val + 1; val = val + 1; c. c = 3; val = val + (c == 3 ? 2 : 3); d. All of the above increment val by 3.
c. c = 3; val = val + (c == 3 ? 2 : 3);
3.7 Q2: What is the difference between a float and a double? double variables store integers and float variables store floating-point numbers. double variables store numbers with smaller magnitude and coarser detail. double variables store numbers with larger magnitude and finer detail. None of the above.
c. double variables store numbers with larger magnitude and finer detail.
2.5.8 Q1: Portions of statements that contain calculations are called variables. constants. expressions. None of the above.
c. expressions.
3.7 Q1: Which two Java primitive types store floating-point numbers? decimal and float. point and double. float and double. decimal and point.
c. float and double.
1.9 Q2 : The command ________ executes a Java application. run javac java None of the above
c. java.
2.5.6 Q1: Which of the following is a Scanner method for inputting an integer value? nextInteger integer nextInt int
c. nextInt.
3.5 Q1: What is the default value of a reference? 0. "". null. default.
c. null.
2.5.4 Q1: Which of the following is not a Java primitive type? char byte real double
c. real
3.4.2 Q1: If a class does not define constructors, the compiler provides a default constructor with no parameters, and the class's instance variables are initialized to ________. zero null their default values. false
c. their default values.
4.10 Q4: Which of the following segments is a proper way to call the method readData four times? a. double k = 0.0; while (k != 4) { readData(); k = k + 1; } b. int i = 0; while (i <= 4) { readData(); i = i + 1; } c. int i = 0; while (i < 4) { readData(); } d. int i = 0; while (i < 4) { readData(); i = i + 1; }
d.
4.13 Q1: Which of the following operators associates from left to right? a. = b. ?: c. %= d. /
d. /
Implementing Counter-Controlled Repetition 4.9 Q2: How many times is the body of the loop below executed? int counter = 1; while (counter > 20) { // body of loop counter = counter - 1; } // end while a. 19. b. 20. c. 21. d. 0.
d. 0.
1.5.3 Q1: Which statement is false? a. Classes are reusable software components. b. A class is to an object as a blueprint is to a house. c. Performing a task in a program requires a method. d. A class is an instance of its object.
d. A class is an instance of its object. The reverse is true.
2.5.5 Q1: Which of the following statements is true? System.out.print("Enter your age: "); prompts the user to take action. Class names typically begin with a capital letter. Package java.lang is imported in every Java program. All of the above are true.
d. All of the above are true.
1.5.5 Q1: Which of the following statements is true? a. When you drive a car, pressing its gas pedal sends a message to the car to perform a task—that is, to go faster. b. You send messages to an object; each message is implemented as a method call that tells a method of the object to perform its task. c. A program might call a bank-account object's deposit method to increase the account's balance. d. All of the above statements are true.
d. All of the above statements are true.
1.5.2 Q1: Which of the following statements is true? a. Performing a task in a Java program requires a method. b. A method houses the program statements that actually perform its tasks. c. The method hides its statements from its user, just as the accelerator pedal of a car hides from the driver the mechanisms of making the car go faster. d All of the above.
d. All of the above.
3.2 Q2: A class instance creation expression contains: Parentheses. The new keyword. The name of the class. d. All of the above.
d. All of the above.
5.2 Q1: Counter-controlled repetition requires A control variable and initial value. A control variable increment (or decrement). A condition that tests for the final value of the control variable. All of the above.
d. All of the above.
Notes on Integer Division and Truncation 4.9 Q4: Which statement is true? a. Dividing two integers results in integer division. b. With integer division, any fractional part of the calculation is lost. c. With integer division, any fractional part of the calculation is truncated. d. All of the above.
d. All of the above.
2.2 Q3: Which of the following cannot cause a syntax error to be reported by the Java compiler? Mismatched {} Missing */ in a comment that begins with /* Missing ; An extra blank line.
d. An extra blank line.
1.5.6 Q1: Which of the following statements is false? a. An object's attributes are specified as part of the object's class. b. A bank-account object would likely have a balance attribute that represents the amount of money in the account. c. Each bank-account object knows the balance in the account it represents, but not the balances of the other accounts in the bank. d. Attributes are specified by the class's methods.
d. Attributes are specified by the class's methods. Acually, attributes are specified by the class's instance variables.
3.2.1 Q6: Which of the following statements is false? The method's return type specifies the type of data returned to a method's caller. Empty parentheses following a method name indicate that the method does not require any parameters to perform its task. When a method that specifies a return type other than void is called and completes its task, the method must return a result to its calling method Classes often provide public methods to allow the class's clients to set or get private instance variables; the names of these methods must begin with set or get.
d. Classes often provide public methods to allow the class's clients to set or get private instance variables; the names of these methods must begin with set or get. Actually, the names of these methods need not begin with set or get, but this naming convention is recommended.
3.2.1 Q2: Which of the following statements is false? By convention class names begin with an uppercase letter, and method and variable names begin with a lowercase letter. Instance variables exist before methods are called on an object, while the methods are executing and after the methods complete execution. A class normally contains one or more methods that manipulate the instance variables that belong to particular objects of the class. Instance variables can be declared anywhere inside a class.
d. Instance variables are declared inside a class declaration but outside the bodies of the class's method declarations.
1.8 Q2: Which of the following statements about Java Class Libraries is false: Java class libraries consist of classes that consist of methods that perform tasks. Java class libraries are also known as Java APIs (Application Programming Interfaces). An advantage of using Java class libraries is saving the effort of designing, developing and testing new classes. Java class libraries are not portable
d. Java class libraries are not portable. (Java class libraries are portable.)
3.2.1 Q5: Which of the following statements is false? Variables declared in the body of a particular method are local variables and can be used only in that method. A method's parameters are local variables of the method. Every method's body is delimited by left and right braces ({ and }). Keyword null indicates that a method will perform a task but will not return any information.
d. Keyword null indicates that a method will perform a task but will not return any information. Actually, keyword void indicates that a method will perform a task but will not return any information.
5.3 Q2: Consider the classes below: public class TestA { public static void main(String args[]) { int x = 2; int y = 20 int counter = 0; for (int j = y % x; j < 100; j += (y / x)) counter++; } } public class TestB { public static void main(String args[]) { int counter = 0; for (int j = 10; j > 0; --j) ++counter; } } Which of the following statements is true? The value of counter will be different at the end of each for loop for each class. The value of j will be the same for each loop for all iterations Both (a) and (b) are true. Neither (a) nor (b) is true.
d. Neither (a) nor (b) is true.
3.4 Q3: What type of methods allow a client of a class to assign values to a private instance variable? Get methods. Replace methods. Assign methods. Set methods.
d. Set methods.
2.3 Q3: Which of the following statements will print a single line containing "hello there"? System.out.println( "hello" ); System.out.println( " there" ); System.out.println( "hello" , " there" ); System.out.println( "hello" ); System.out.print( " there" ); System.out.print( "hello" ); System.out.println( " there" );
d. System.out.print( "hello" ); System.out.println( " there" ); 2.3 Q4: Which of the following escape sequences represents a carriage return? \n. \r. \cr. \c. ANS: b. \r.
Application: Compound Interest Calculations 5.4 Q4: Which statement prints the floating-point value 123.456 right justified with a field width of 10? System.out.printf("%d10.3", 123.456); System.out.printf("%10.3d", 123.456); System.out.printf("%f10.3", 123.456); System.out.printf("%10.3f", 123.456);
d. System.out.printf("%10.3f", 123.456);
3.4.2 Q2: Which of the following statements is false? If a class does not define constructors, the compiler provides a default constructor with no parameters. If you declare a constructor for a class, the compiler will not create a default constructor for that class. The UML models constructors in the third compartment of a class diagram. To distinguish a constructor from a class's operations, the UML places the word "constructor" between double quotes before the constructor's name.
d. To distinguish a constructor from a class's operations, the UML places the word "constructor" between double quotes before the constructor's name. Actually, the UML places the word "constructor" between guillemets (« and ») before the constructor's name.
2.5.4 Q2: Which of the following statements is false? Primitive types are keywords. Primitive types must appear in all lowercase letters. Real numbers contain decimal points. Variable name identifiers must begin with a lowercase letter.
d. Variable name identifiers must begin with a lowercase letter. This is not required, but it is a convention.
3.3 Q2: Which of the following statements is false? A primitive-type variable can store exactly one value of its declared type at a time. Primitive-type instance variables are initialized by default. Variables of types byte, char, short, int, long, float and double are initialized to 0. Variables of type boolean are initialized to true.
d. Variables of type boolean are initialized to true. Actually, variables of type boolean are initialized to false.
3.5.1 Q1: A __________ of a class called myClass is another class whose methods call the methods of myClass. a. consumer b. servant c. caller d. client
d. client
4.14 Q3: Which primitive type can hold the largest value? a. int b. long c. float d. double
d. double
5.4 Q2: Which of the following will count down from 10 to 1 correctly? for (int j = 10; j <= 1; j++) for (int j = 1; j <= 10; j++) for (int j = 10; j > 1; j--) for (int j = 10; j >= 1; j--)
d. for (int j = 10; j >= 1; j--)
3.3 Q4: Which of the following is a valid fully qualified name? Scanner. java.Scanner. util.Scanner. java.util.Scanner.
d. java.util.Scanner.
3.2.1 Q4: When a method terminates, the values of its local variables are ________. saved copied restored lost
d. lost
3.4 Q2: What is the default initial value of a String instance variable? "" "default" default null
d. null
2.5.1 Q2: Java's predefined classes are grouped into packets. declarations. Galleries. packages.
d. packages.
3.2 Q1: Each class you create becomes a new ________ that can be used to declare variables and create objects. package instance library type.
d. type.
2.5 Q1: Programs remember numbers and other data in the computer's memory and access that data through program elements called comments. messages. integers. variables.
d. variables.
2.5.2 Q2 The body of each class declaration begins with ________ and ends with ________. (, ). [, ]. {, }. /, \.
d. {, }.