Module 05: Writing Classes Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

what will this code print?

Person myPerson = new Person(“Bob”);

myPerson.changeName(“Joe”);

myPerson.name = “John”;

myPerson.printName();

A

John

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

Given this code snippet,

public class Athlete

{

public Athlete(String name)

{

this.name = name;

}

}

what is missing from the class definition?

  1. Need to declare the name instance variable
  2. Missing void in constructor definition
  3. Class constructors must be private
  4. Java does not use the keyword this, instead it uses self
  5. No constructor is defined for the class
A
  1. Need to declare the name instance variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is wrong with the class definition?

Hint : We expect the instance variables name and health to be initialized in the constructor.

  1. Missing void in constructor definition
  2. Class constructors must be private
  3. The constructor parameters cannot have the same names as the instance variables.
  4. Must use this in constructor when the constructor parameters have the same name as instance variables. ie:
    this. name = name;
    this. health = health;
A
  1. Must use this in constructor when the constructor parameters have the same name as instance variables. ie:
    this.name = name;
    ​this.health = health;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the output running Main.bar();?

A

3

The instance variable n Is declared static, so it is shared by all instances of class Main. Since n is incremented by each constructor, the value of n after three instantiations is 3. It does not matter on which instance foo() is called.

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

Refer to this code snippet.

A

5

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

What is the output of the following program?

  1. The program does not compile.
  2. The program does not produce any output.
  3. foo
  4. bar
A
  1. bar

In Main(String str), the parameter name string shadows the instance variable name string. The statement string = string assigns the parameter string to itself, and so the assignment has no effect outside of Main(String str). Therefore, getString() will return the static variable bar.

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

Given an instance of the Athlete class called athlete, what is the proper way to set the value of the jersey number after it has been instantiated?

  1. athlete.jersey = 23;
  2. athlete.getJersey() = 23;
  3. athlete.getJersey(23);
  4. athlete.setJersey(23);
  5. You cannot set the jersey, since jersey is private and there is no setter method.
A
  1. You cannot set the jersey, since jersey is private and there is no setter method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given an instance of the Athlete class called athlete, what is the proper way to get the value of the jersey number?

  1. athlete.jersey
  2. athlete.jersey()
  3. athlete.getJersey
  4. athlete.getJersey()
  5. None of the above
A
  1. athlete.getJersey()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Mark the valid way to create an instance of Athlete given the following Athlete class definition:

  1. athlete = new Athlete(“Dirk”, “Nowitzki”, 41);
  2. Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, 41);
  3. Athlete athlete = new Athlete(“Dirk” “Nowitzki” 41);
  4. Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, “41”);
A
  1. Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, 41);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Mark the valid way to create an instance of Foo given the following code:

  1. Foo fee = Foo(32);
  2. Foo fee;
    new fee = Foo();
  3. Foo fee = new Foo(10, “You.”)
  4. Foo fee;
    fee = new Foo();
A
4. Foo fee;
​fee = new Foo();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given the following definition for the class Athlete:

Which of the following are valid instantiations of the class Athlete?
I – Athlete joe = new Athlete("Joe", "Montana");
II – Athlete joe = new Athlete("Joe, "Montana", "16");
III – Athlete joe = new Athlete("Joe", "Montana", 16);

I and III

I only

II only

I II and III

II and III

A

I and III

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

Which methods of class Foo can be called without an actual instance of the class Foo?

  1. All methods require an instance of Foo in order to be called.
  2. No methods require an instance of Foo in order to be called.
  3. bar()
  4. baz()
  5. foo()
A
  1. foo()

foo() is declared static, so it can be invoked on the class, without an instance.

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

Which of the following explains why this code will not compile?

  1. The constructor is missing a return type and it should be set to void.
  2. The constructor is missing a return type and it should be set to boolean.
  3. The instance variables name and channel should be public.
  4. The return type for the getName method should be set to String.
  5. The return type for the setName method should be set to String.
A
  1. The return type for the getName method should be set to String
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which statement best describes this code?

  1. The code will not compile because there are two constructors with the same signature.
  2. The code will not compile because the constructors do not have a return type.
  3. The code will compile, but will not run because there are multiple constructors.
  4. The code will compile, but will not run because the variables are not initialized.
  5. The code will compile and work as intended.
A
  1. The code will not compile because there are two constructors with the same signature.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which of the following would be the best example of how the Internet has impacted the economy?

  1. Social networking has allowed friends to keep in touch even when they move apart.
  2. People rely on email and texting to keep in touch instead of phone calls.
  3. Email has reduced the amount of mail that is produced and shipped.
  4. Superstores such as Wal-Mart and Target have caused a decline in the smaller family owned stores.
A
  1. Email has reduced the amount of mail that is produced and shipped
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following is not part of the ACM Code of Ethics and Professional Conduct’s General Ethical Principles?

  1. Respect privacy.
  2. Credit original creator when using other’s work.
  3. Be honest and trustworthy.
  4. Be fair and take action not to discriminate.
A
  1. Credit original creator when using other’s work
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Consider the following code segment:

public static String mystery(String word, int i, int j)

{

String mystery = word.substring(i, i+ 1);

mystery += word.substring(i, j);

return mystery;

}

Which of the following is the most appropriate precondition for the variable i in mystery so that substring does not throw an exception?

  1. Precondition: i >= 0, i < word.length, i <= j.
  2. Precondition: i >= 0, i <= word.length, i <= j.
  3. Precondition: i > 0, i < word.length, i < j.
  4. Precondition: i >= 0, i < word.length, i > j.
  5. Precondition: i > 0, i <= word.length.
A
  1. Precondition: i >= 0, i < word.length, i <= j
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Consider the following code segment:

public static String mystery(String word, int i, int j)

{

String mystery = word.substring(i, i+ 1);

mystery += word.substring(i, j);

return mystery;

}

Which of the following is the most appropriate precondition for the variable j in mystery so that substring does not throw an exception?

  1. Precondition: j > i and j <= word.length.
  2. Precondition: j >= i and j <= word.length.
  3. Precondition: j >= i and j < word.length.
  4. Precondition: j > 0 and j <= word.length.
  5. Precondition: j < i and j <= word.length.
A
  1. Precondition: j >= i and j <= word.length
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the difference between a public and private method/variable?

A

Public: allow access to data and methods from classes outside the declaring class

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

What is Encapsulation?

A

The process of hiding the implementation details of a class from the user

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

What are accessor methods?

A

Methods used to access instance variables and object data. Also referred to as getter methods

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

What are mutator methods?

A

Methods used to change or manipulate instance variables or object data. Also referred to as setter methods.

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

What are access specifiers?

A

Access Specifiers: Determine whether classes, data, constructors, and methods can be accessed outside of the declaring class

Public: allows access from classes outside the class

Private: restricts access to the declaring class

  • Accessors and Mutators: Allow private data to be accessed outside of the class and be safely modified.
24
Q

What is public access?

A

public Access:

  • Designed as public so that they can be accessed outside of the class file
  • Class members should be public:
  • Constructors
    • Accessors/Mutators
    • Methods that user needs to manipulate the object of the class

public class Rectangle

{

public Rectangle()

}

25
Q

What is the difference between encapsulation and abstraction?

A

Encapsulation: The process of hiding implementation details is referred to as encapsulation

Encapsulation is different than Abstraction because it is concerned with hiding the internal state of an object, whereas Abstraction is concerned with hiding the details of how something works

26
Q

What are instance variables?

A

Access to attributes should be kept internal to the class

Instance variables are designated as private so as to prevent users from directly manipulating the state of an object

27
Q

What is an alias?

A

A variable that references an existing object.

When the alias variable is manipulated, so is the original object, and visa versa

28
Q

What is a “has-a” relationship?

A

Objects are defined by having the attributes, or instance variables that they are assigned.

29
Q

What is the state of an object?

A

The state of an object refers to its attributes and values.

  • Object’s attributes are defined by the class’s instance variables
  • Set the initial state of an object and its instance variables using the object’s Constructor:
  • Constructor parameters are local variables that provide data to initialize instance variables*
  • If no constructor is written for a class, Java provides a no-argument constructor and sets the instance variables to a default value:
30
Q

What is a constructor?

A

Constructor parameters are local variables that provide data to intialize instance variables

31
Q

What is a single-line comment?

A

// This is a single line comment

32
Q

What is a multi-line comment?

A

/* This is a multi-

line comment */

Allows us to easily write additional comment lines for longer comments

33
Q

What are Javadoc Comments?

A

/**

  1. One sentence description of the code’s function
  2. Preconditions (Conditions that must be true prior to execution in order for that code segment to behave as expected in order for the code segment to behave as expected)
  3. Postconditions (Describes the outcome of the execution, such as what is returned or changed)
  4. Block tags

@param - parameters

@return - return value

*/

Used when writing longer code and providing clarification on the code

34
Q

What are block tags?

A

Format: @tag

What: Provides additional information about the code segment Common block tags are @param and @return which indicates the parameters ad return values of the code segment

35
Q

What is accessor methods?

A

A method that enables users to obtain information about an object’s instance and static variables.

  • Rather than allow users to manipulate data directly, we use accessors (getters) and mutators (setter) methods to control what aspects of an object can be altered
  • Accessors = return value

must match method signature return type

If the return value is an object, it returns a copy of the existing reference, NOT a new copy of the object

36
Q

What is the toString Method?

A

A specific accessor method that returns a String value with information about an object’s instance values. This overrides the object’s inherent toString method when an object is printed.

  • toString() methods are accessor methods that provide information on the instance data of an object
  • When an object is printed, toString() method is called and the returned String is printed
37
Q

What are mutator methods?

A

Mutator methods are often void methods that change the value of instance and static variables

  • Often void methods that change the value of instance and static variables
  • Don’t necessarily need parameters

public void setWidth(int newWidth)

//If a method has no return value - return type is void

{

width = newWidth;

// Mutator methods alter the value of instance and static variables

}

38
Q

What is object.InstanceVariable?

A

Instance variables can be accessed directly by using the reference variables the instance variable name.

This only works within in the class file if the instance variable are set to private

39
Q

What are formal parameters?

A

Formal Parameters: listed parameters that can be passed to the method, while the actual parameters are the values that are passed to the method

  • Parameter data types do not have to match the return type
  • When the method is called, the actual parameters must match the formal parameter data type
40
Q

What are primitive parameters?

A

When the actual parameter is a primitive value, the formal parameter is initialized with a copy of that value.

  • Changes to the formal parameter have no effect on the corresponding actual parameter.
  • The original value is altered after being used as a parameter
  • The reference variable changes because the formal parameter is an alias of the actual parameter

Methods can access the private data and methods of reference parameters if the parameter is the same type as the class it belongs to

  • Trying to access private data from formal parameters will cause an error if the method is outside the object’s class
41
Q

What are reference parameters?

A

When an actual parameter is a reference to an object, the formal parameter is initialized with a copy of that reference, not a copy of the object

42
Q

Math.abs(x)

A

Returns the absolute value of x

43
Q

Math.pow(base, exponent)

A

Returns the value of base raised to the power of the exponent

44
Q

Math.sqrt(x)

A

Returns the positive square root of x

45
Q

Math.random()

A

Returns a double value greater than or equal to 0.0 and less than 1.0

46
Q

What is a static variable?

A

Variables that can be accessed by all objects of a class.

  • They are called using the class name and can be used in static and non-static methods
  • They are referenced by the class name itself.
47
Q

What is a static method?

A

Methods that can be used directly by the class name.

They cannot access instance variables or non-static methods

48
Q

What is the difference between static and instance variables?

A

Because static methods belong to the class, and not an object, they cannot access instance variables or non-static methods of the class

  • Static methods can use other static methods from the same class
49
Q

What is method decomposition?

A

The process of breaking down large problems into smaller problems, each with a method that defines a subproblem in the larger problem

50
Q

What is shadowing?

A

I two variables within the same scope have the same name, the variables with the more specific scope will be called.

51
Q

What are local variables?

A

A variable that defines in a method or constructor.

  • It only exists in the context of the method it belongs to
  • Cannot be declared public or private
52
Q

What is the scope?

A

Scope: Variable exists from the point where it is declared until the end of the block it is declared inside of

53
Q

What is “this” keyword?

A

⌨️ Avoid Naming conflicts by using the keyword this to specify which variable we are referring to

  • In a non-static method this reference to the current object whose methods and constructors are being called.
  • this specifies that the variable or method being called belongs to the existing object
  • this can also be used to call an object’s method
54
Q

How can “this” be used with methods?

A
  • Object is calling the method is the one referred to as this:
  • this can also be used as an actual parameter to pass the current object to a method
  • Method will use the attributes and methods of the current object:
55
Q

What is system reliability?

A

⌨️ System Reliability: when all programs and code will work as intended

Artificial Intelligence:

  1. enhance automated process
  2. Reduce human safety risk
  3. Ethical Implications
56
Q

What is the Code of Ethics?

A
  1. Contribute to society and to human well-being, acknowledging that all people are stakeholders in computing.
  2. Avoid harm.
  3. Be honest and trustworthy.
  4. Be fair and take action not to discriminate.
  5. Respect the work required to produce new ideas, inventions, creative works, and computing artifacts.
  6. Respect privacy.
  7. Honor confidentiality.