Shared Flashcard Set

Details

Java CS170 Chapter. 11 - 12
Quiz Questions
14
Computer Science
Undergraduate 2
12/07/2011

Additional Computer Science Flashcards

 


 

Cards

Term
When you instantiate an object from a class, ____ is reserved for each instance variable (field) in the class.


Answer

A. a signature

B. a field name

C. a constructor

D. memory
Definition
D. memory
Term
A(n) ____ constructor is one that requires no arguments.
Answer

A. class

B. explicit

C. default

D. write
Definition
C. default
Term
Methods that retrieve values are called ____ methods.
Answer

A. class

B. accessor

C. static

D. mutator
Definition
B. accessor
Term
If a class’s only constructor requires an argument, you must provide an argument for every ____ of the class that you create.
Answer

A. Object

B. parameter

C. Type

D. Method
Definition
A. Object
Term
Which of the following is NOT an actual parameter?
Answer

A. Constant values used in a method call

B. Variables defined in a method heading

C. Expressions used in a method call

D. Variables used in a method call
Definition
D. Variables used in a method call
Term
Given the method heading below, which of the following would be an incorrect demonstration of method overloading?

int larger(int x, int y)
Answer

A. int larger(int x, int y, int z)

B. double larger(double x, double y)

C. int larger(int a, int b)

D. char larger(char x, char y, char z)
Definition
C. int larger(int a, int b)
Term
Given the method heading below, which of the following does NOT demonstrate method overloading?

int larger(int x, int y)
Answer

A. int larger(int x, int y, int z)

B. int larger(char x)

C. double larger(double x, double y)

D. int max(int x, int y)
Definition
D. int max(int x, int y)
Term
Based on the code below, which of the following identifiers is visible in block three?

public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;

public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}

public void one(int f, char g) //Line 15
{
// block one...
}

public static int w; //Line 20

public void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25

//block three
{
int z = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Answer

A. z (before main)

B. x (one’s formal parameter)

C. local variables of main

D. t (before main)
Definition
D. t (before main)
Term
Based on the code below, which of the following identifiers is NOT visible in method two?

public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;

public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}

public void one(int f, char g) //Line 15
{
// block one...
}

public static int w; //Line 20

public void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25

//block three
{
int z = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Answer

A. w (before method two)

B. one (method name)

C. rate (before main)

D. x (variable in block three)
Definition
D. x (variable in block three)
Term
When they have the same name, variables within ____ of a class hide instance variables.
Answer

A. fields

B. packages

C. statements

D. methods
Definition
D. methods
Term
Based on the code below, where is identifier z (block three’s local variable) visible?

public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;

public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}

public void one(int f, char g) //Line 15
{
// block one...
}

public static int w; //Line 20

public void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25

//block three
{
int z = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Answer

A. In block three only

B. In block three and main

C. In two and block three

D. In one and block three
Definition
A. In block three only
Term
Given the class definition below, which of the following statements would you use to declare a new reference variable of the type Illustrate and instantiate the object with a value of 9?

public class Illustrate
{
private int x;
private static int y;
public static int count;
public static int z;

public Illustrate()
{
x = 1;
}

public Illustrate(int a)
{
x = a;
}

public void print()
{
System.out.println("x = " + x + ", y = "
+ y + ", count = " + count);
}

public static void incrementY()
{
y++;
}
}
Answer

A. Illustrate.illObject(9);

B. Illustrate illObject(9);

C. Illustrate illObject = new Illustrate(9);

D. illObject = Illustrate(9);
Definition
C. Illustrate illObject = new Illustrate(9);
Term
Which of the following class definitions is correct in Java?

// Example (i)
public class Student
{
private String name;
private double gpa;
private int id;

public void Student()
{
name = "";
gpa = 0;
id = 0;
}

public void Student(String s, double g, int i)
{
set(s, g, i);
}

public void set(String s, double g, int i)
{
name = s;
gpa = g;
id = i;
}

public void print()
{
System.out.println(name + " " + id + " " + gpa);
}
}

// Example (ii)
public class Student
{
private String name;
private double gpa;
private int id;

public Student()
{
name = "";
gpa = 0;
id = 0;
}

public Student(String s, double g, int i)
{
set(s, g, i);
}

public void set(String s, double g, int i)
{
name = s;
gpa = g;
id = i;
}

public void print()
{
System.out.println(name + " " + id + " " + gpa);
}
}
Answer

A. Only (i)

B. Only (ii)

C. Both (i) and (ii)

D. None of these
Definition
B. Only (ii)
Term
Based on the class definition below, which of the following statements is illegal?

public class Illustrate
{
private int x;
private static int y;
public static int count;
public static int z;

public Illustrate()
{
x = 1;
}

public Illustrate(int a)
{
x = a;
}

public void print()
{
System.out.println("x = " + x + ", y = "
+ y + ", count = " + count);
}

public static void incrementY()
{
y++;
}
}
Answer

A. Illustrate.count++;

B. Illustrate.z++;

C. Illustrate.incrementY();

D. Illustrate.x++;
Definition
D. Illustrate.x++;
Supporting users have an ad free experience!