CH.9 QUIZ QUESTIONS

5 September 2022
4.7 (114 reviews)
29 test answers

Unlock all answers in this set

Unlock answers (25)
question
A constructor can access __________. - a local variable defined in any method - a private instance variable - a public instance variable - a static variable
answer
- a private instance variable - a public instance variable - a static variable
question
A method that is associated with an individual object is called ____________. - a static method - a class method - an instance method - an object method
answer
an instance method
question
A static data field can be accessed from any method in the same class. True or false?
answer
True
question
A static method in a class can access the class variables in the same class? True or false?
answer
True
question
A static method in a class can access the instance variables in the same class? True or false?
answer
False
question
All data fields in an object have default values True or false?
answer
True
question
An immutable class cannot have ____________ - public data fields - private data fields - public constructors - no-arg constructors - static data fields
answer
public data fields
question
An object is an instance of a _________. -program -class -method -data
answer
Class
question
Analyze the following code and choose the best answer: public class Foo { private int x; public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } } A-Since x is private, it cannot be accessed from an object foo. B-Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code. C-Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. D-You cannot create a self-referenced object; that is, foo is created inside the class Foo.
answer
C- Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.
question
Analyze the following code: class Test { private double i; public Test(double i) { this.t( ); this.i = i; } public Test( ) { System.out.println("Default constructor"); this(1); } public void t( ) { System.out.println("Invoking t"); } } A- this.t( ) may be replaced by t( ). B- this.i may be replaced by i. C- this(1) must be called before System.out.println("Default constructor"). D- this(1) must be replaced by this(1.0).
answer
A and C - this.t( ) may be replaced by t( ). - this(1) must be called before System.out.println("Default constructor").
question
____________ is invoked to create an object. A constructor The main method A method with a return type A method with the void return type
answer
A constructor
question
________ can be accessed from any instance method in the class. - A local variable - An instance variable - A static variable
answer
An instance variable A static variable
question
________ can be accessed from any static method in the class. - A local variable - An instane variable - A static variable
answer
A static variable
question
You can use the ______ operator to access members of an object - . - ( ) - * - %
answer
.
question
You should add the static keyword in the place of ? in Line ________ in the following code: public class Test { private int age; public ? int square (int n) { return n * n; } public ? int getAge( ) { return age; } }
answer
Both first and second ?'s
question
You cannot use the private modifier on classes True or false?
answer
False
question
You can declare two variables with the same name in ________. -a method one as a formal parameter and the other as a local variable -a block -two nested blocks in a method (two nested blocks means one being inside the other) -different methods in a class
answer
Different methods in a class
question
Which of the following statements are true? -A default constructor is provided automatically if no constructors are explicitly declared in the class. -At least one constructor must always be defined explicitly. -Every class has a default constructor. -The default constructor is a no-arg constructor.
answer
A default constructor is provided automatically if no constructors are explicitly declared in the class.
question
Advantage of encapsulation? A -Only public methods are needed B -Making the class final causes no consequential changes to other code. C -It changes the implementation without changing a class's contract and causes no consequential changes to other code. D -It changes a class's contract without changing the implementation and causes no consequential changes to other code.
answer
C It changes the implementation without changing a class's contract and causes no consequential changes to other code.
question
* * * What is the value of times displayed? public class Test { public static void main (String[] args) { Count myCount = new Count( ); int times = 0; for (int i=0; i < 100; i++) increment(myCount, times); System.out.println( "myCount.count = " + myCount.count); System.out.println("times = " + times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { int count; Count(int c) { count = c; } Count( ) { count = 1; } } 101 100 99 98 0
answer
times = 0 myCount.count = 101
question
* * * What is the printout of the second println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo( ); System.out.println("f1.i is" + f1.i + "f1.s is" + f1.s); Foo f2 = new Foo( ); System.out.println("f2.i is" + f2.i + "f2.s is" + f2.s); Foo f3 = new Foo( ); System.out.println("f3.i is" + f3.i + "f3.s is" + f3.s); } public Foo( ) { i++; s++; } } A- f2.i is 1 f2.s is 1 B- f2.i is 1 f2.s is 2 C- f2.i is 2 f2.s is 2 D- f2.i is 2 f2.s is 1
answer
B So because "i" is an instance variable and not a class variable, it will only increment once. But s is a class variable (in fact, you can use s and not f1.s or f2.s or f3.s) and because its a class variable it will increment each time Foo objects are invoked.
question
* * * Printout for the first statement in the main method? public class Test { private int i = 0; static int j = 0; public static void main(String[] args) { new Test( ); } public Test( ) { i++; j++; int i = 1; int j = 1; System.out.println("i is " + j + " j is " + j); } }
answer
i is 1 j is 1 So this is because of the int i = 1 and int j = 1 statements which overwrite anyhting else and set i and j both to 1. If we had made two lines in the main so there would be new Test TWICE, then we would see i only increment once because its a class variable but j would increment twice because its a static variable, or class variable
question
Suppose you declare Date d. d is now called __________. -an object -a reference variable for an object - an object value - a variable that holds an integer val
answer
a reference variable for an object
question
Each class in the file is compiled into a separate bytecode file True or false?
answer
True
question
The pillars of object oriented programming are?
answer
encapsulation, polymorphism, inheritance
question
A super class is referred to as a _________ class and sub class is referred to as a __________.
answer
parent; child
question
What does polymorphism mean?
answer
Means that a variable of a supertype can refer to a subtype object
question
What is object oriented programming? Or inheritance?
answer
Allows you to define new classes from existing classes, or inheritance
question
Who is Ada Lovelace?
answer
English mathematician and write; worked on early mechanical general-purpose computer for Charles Cabbage; her work involved the first algorithm to be carried out by a machine; first computer programmer