Comp111-Final Exam Flashcards

1
Q

What is a sentinel value?

A

A constant used in a loop, so that when that value is received as input, the loop terminates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
boolean stopLoop = false;
int ctr = 0;
while (stopLoop){
   if(ctr++ > 12){
      System.out.println(ctr);
   }
}
  • how many times will this loop execute?
  • What will be the output from this ?
A
  • 0 times

- nothing, null

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

Describe the four types of loops. When would each be used? Know how to write each type of loop.

A
while - pretest loop, as the condition is tested before the execution of the body. If false, it never runs.
initialize
while(condition){
   body
   update 
}

for - ideally used for counting, especially in the case of an array. During each step of the loop, the body is executed, & the update is to increment the variable.
for(initialize; condition; update){
body
}

do/while - used when loop needs to run an indeterminate amount of times, where the body needs to execute before the condition is tested.
variable
do{
   body
   update
} while(condition);

enhanced for - used to traverse through the elements of an array or ArrayList.
for(dataType variable : collection){
statement;
}

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

Can a for loop be rewritten as a while loop? As a do/while loop?

A
  • Yes

- Yes

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

What are the four parts of a loop?

A

initialization, condition, body, update

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

Write the while loop statement to print the value for each element in an array of type String named Dogs.

A
int i = 0;
while(i < Dogs.length){
   System.out.println(Dogs[i]);
   i++;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write the for loop statement to print the value for each element in an array of type String named Dogs.

A

for(int i = 0; i < Dogs.length - 1; i++){
System.out.println(Dogs[i]);
}

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

Write the enhanced for loop statement to print the value for each element in an array of type String named Dogs.

A

for(String dog : Dogs){
System.out.println(dog);
}

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

Write the code segment to prompt a user for entry of a double value until a sentinel value of -99.99 is entered. After the sentinel value, compute the average of the numbers entered.

A
double sent = 99.99;
double i = 0;
int count = 0;
while(i < sent){
   System.out.println("Enter value: ");
   Scanner scanDbl = new Scanner(System.in);
   double inDbl = scanDbl.nextDouble();
  if((i + inDbl) > sent){
     System.out.println("too large");
   }else if(((I + inDbl) <= sent){
      i = i + inDbl;
      if(i == sent){
         System.out.println("max value reached");
         System.out.println(“average is: “ + (sent / (double)count));
         return;
      }
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Evaluate the following code:
Random generator = new Random();
int newNumber = 5 + generator.nextInt(10);
o	What possible values will the random number generater return based upon the above explicit paramter of 10?
o	What possible values will the integer variable newNumber contain after these statements execute?
A
  • Random number between 0 & 9

- Random number between 5 & 14

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

Write the statement to generate a random number that represents a roll of a die. Where each side of the die is represented by the numbers 1 through 6, the 1 is number 1, the 2 is number 2, etc.

A
Random generator = new Rnadom();
int d = 1 + generator.nextInt();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

• Describe wrapper and autoboxing. What are the wrapper classes for int, double, boolean, and char?

A

Wrapper Class is because Arraylist cannot take in primitive data types. Autoboxing is the process of changing primitives to a Wrapper Class. Integer, Double, Boolean, & Character.

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

• How do you convert a String variable to an integer using the wrapper class?

A

valueOf() to get an int.

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

• How do you convert a String variable to a double using the wrapper class?

A

valueOf() to get an double

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

• Evaluate the following statement, assuming that testArray is defined with a length of 10 and a data type of double.
testArray[10] = 104.89;
o What are the boundaries of the array?
o Is the double value being inserted within the boundaries?
o Will this statement compile? If no, why not?
o Will an error be produced at run time? if yes, what error?

A
  • 10 array elements available, 0-9
  • No
  • Yes, because it is correct in respect to syntax.
  • No, as the array position 10 is out of bounds.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

• Describe an array verses and ArrayList.

o What are the advantages and disadvantages of both?

A

Array is a fixed length data structure. Whereas Arraylist is a variable length. Array is useful when the amount needed for the structure is known, but it cannot be adjusted. ArrayLists can grow & shrink as needed, and come with built in methods.

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

• What type of class is an ArrayList?

A

Collection Class

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

• What methods are available with an ArrayList?

A

size(), get(), add(), indexOf(), contains(), set(), remove(), sort(), clear()

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

• Write the following declaration statements:
o Declare an array of 100 Coin objects with a name of tmpCoin.
o Declare an array of 50 int values with a name of dogs.
o Declare an array of 75 Rat objects with a name of compRats.
o Declare a 4 element integer array with a name of scores, and assign the whole numbers 1 through 4 to the array elements.
o Declare an ArrayList of data type String and a name of employees.
o Declare an ArrayList of Paycheck objects with a name of AnnualPay.

A
  • Coin [] tmpCoin = new Coin[100];
  • int [] dogs = new int[50];
  • Rats [] compRats = new Rats[75];
  • int [] scores = new int[4];
    scores = {1, 2, 3, 4};
  • ArrayList employees = new ArrayList();
  • ArrayList AnnualPay = new ArrayList();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

• How do you return the length of a
o String?
o array?
o ArrayList?

A
  • String.length()
  • array.getLength();
  • ArrayList.size();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

• How do you know how many elements within an array have been populated?

A

Establish an int variable to count the elements that have been added to the array. You can then compare it to the array.getLength().

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

• Write the code to populate an ArrayList with the numbers 0 through 85, where each element is the array list is assigned 1 number. The first element is assigned 0, the second assigned 1, and so on.
o Write the code using a for loop.
o Write the code using a while loop.
o Write the code using a do/while loop.

A
- int sent = 85;
for(int i = 0; i <= sent; i++){
   ArrayList.add(i);
   i++;
}
- int sent = 85;
int i = 0;
while(i <= sent){
   ArrayList.add(i);
   i++;
}
- int sent = 85;
int i = 0;
do{
   ArrayList.add(i);
   i++;
}while(i <= sent);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

• What is the purpose of the enhanced for loop?

A

For traversing all elements of an array or ArrayList.

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

• Assuming you have an array list of data type string named dogs, write an enhanced for loop to read the entire array list and print out the content of each array element.

A

for(String dog: dogs){
System.out.println(dog);
}

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

• Declare a two-dimensional array that has 3 rows and 4 columns of integer values with a name of testScores.

A

int row = 3;
int col = 4;
int[][] testScores = new int[row][col];

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

• Write a loop to populate each element within the array with a value of 0.

A
int v = 0;
for(int i = 0; i < row; i++){
   for(int j = 0; i < col; j++){
      int[i][j] = v;
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

• What is an actor class?

A

A class that performs a certain task, like Scanner or Random

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

• What does an actor class model, a noun or a verb?

A

verb

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

What is a utility class?

A

Utility are classes that provide services, like Math

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

What is cohesion

A

Is a measure of the degree to which a class models simple concepts.

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

When designing a class is high or low cohesion desired?

A

High cohesion is desired

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

What is coupling?

A

Coupling is a measure of the degree to which a class depends on other classes to work properly.

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

What is a package?

A

Packages group classes with similar purposes into a structure that is more manageable for programmers.

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

• When designing a package (group of classes) is high or low coupling desirable?

A

low coupling

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

• What is an immutable class?

A

A class that defines no mutators

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

What is a mutator class?

A

A class that changes the state of an object

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

What is a side effect?

A

Any kind of modification that is observable outside of the method.

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

What is a precondition?

A

Must be true before a method executes, enforced by stat validation outside the method.

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

What is a postcondition?

A

What is true after the method executes.

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

• Would a JUnit assert method be best to test a precondition or postcondition?

A

For testing the postcondition.

41
Q

What is a static method?

A

Method that is not invoked on an object. Called directly from the class. Like Math.abs().

42
Q

• Does a static variable belong to a class or to an object?

A

Class

43
Q

• Where is a local variable defined?

A

A variable defined within a method, and only accessible within that method.

44
Q

• When does a local variable cease to exist?

A

When the method has completed.

45
Q

• Can a local variable have the same name as an instance variable?

A

Yes, but not ideal

46
Q

• If it can, which variable will be used?

A

The local

47
Q
•	Identify the local variables in the following code:
public void calcSomething(double inNum1, int inNum2)
{
double newNum1 = 0.0;
int newNum2 = 0;
for (int i = 0; i < 100; i++)
{
if (i %2 = 0)
{
double tmpNum1 = inNum1 + inNum2)
}
}
}
A
  • tmpNum1
48
Q
•	Review the following code:
// instance variable
private String zipCode;
public void setZipCode( String zipCode)
{
zipCode = "43215";
}
o	Will the instance variable zipCode or the explicit parameter zipCode be set equal to the value "43215"?
A

the local

49
Q
•	Review the following code.  Which definitions of tmp are legal, meaning they will compile? And which are illegal, meaning they will not compile?
public class X
{
private String tmp;
public String method1()
{
String tmp = "hello";      is this legal?
return tmp;
}
public void method2(String next)
{
int ctr = 0;
boolean tmp = true;      is this legal?
while (tmp)
{
ctr++;
if (ctr > 10) tmp = false;
}
}
public void method3(String tmp)    is this legal?
{
int ctr = 0;
boolean tmp = true;      is this legal?
while (tmp)
{
ctr++;
if (ctr > 10) tmp = false;
}
}
public String method4(String tmp)
{
if (tmp.equals("hello")
{
String tmp = "new value";    is this legal?
return tmp;
}
}
A

The second boolean tmp & String tmp = “new value”.

50
Q

• When viewing a task description, what do the nouns represent? What do the verbs represent?

A
  • Nouns correspond to classes

- Verbs correspond to methods

51
Q

• Draw a UML dependency diagram for a task description.

A

[Customer]—–>[Address]

52
Q

• Viewing the following UML diagram, what classes are dependent upon other classes?

A

The class that arrow to it is what the other class is dependent upon.

53
Q

• What is a sorting algorithm?

A

An algorithm that orders elements in a collection according to a particular criteria.

54
Q

• How does the insertion sort work?

A

It chooses the next unsorted element and finds where to insert it in the sorted selection.

55
Q

• How does the bubble sort work?

A

Slices the array into sorted & unsorted elements, but but compares each pair of adjacent elements from left to right, if out of order, it will swap them.

56
Q

• How does the merge sort work?

A

The merge sort sorts an array by cutting it in half, recursively sorting each half, and then merge the sorted halves.

57
Q

• How does the selection sort work?

A

The selection sort sorts an array by repeatedly finding the smallest element of the unsorted tail region and moving it to the front.

58
Q

• What are the 2 ways in which complexity is usually measured in computer science?

A

Space and time

59
Q

• How does the binary search work?

A

Locates a value in a sorted array by determining whether the value occurs in the first or second half, then repeating the search in one of the halves.

60
Q

• Describe the various operations performed by the CPU.

A

Performs data processing and program control

61
Q

• Describe the different between primary and secondary storage. Be able to provide an example of each.

A

Primary memory, RAM, is the main memory used, the most expensive, data lost without power.
Secondary memory, hard drive, not as fast, less expensive, retains data without power.

62
Q

What is an algorithm?

A

A sequence of instructions that are unambiguous, executable, and terminating.

63
Q

What is psuedo code?

A

Plain language description of programming language, which outlines the functions of a program. Often used as an initial step, before starting programming.

64
Q

• Describe both syntax and logic errors.

A

Syntax errors are errors within the code itself, that will prevent it from compiling. Logic errors occur when the program result is different than what is expected.

65
Q

• What are the two most important benefits of the Java language?

A

It is designed to be safe & portable.

66
Q

• What is the file extension for a java source file? What is the file extension for a compiled java file? How do you compile/call a java executable from the command line?

A

.java

.class

67
Q

• What will be the output of the following statements:
String firstWord = “Hello!”;
String secondWord = “Study”;
String thirdWord = “hard.”;
String newSentence = firstWord + secondWord + thirdWord;
System.out.println(newSentence);

A

Hello!!StudyHard

68
Q

• Evaluate the following code:
String firstWord = “Hello!”
secondWord = “Study”;
String thirdWord = “hard.”;
String newSentence = firstWord & secondWord + thirdWord;
System.out.println(newSentence);
o Identify the syntax errors in the above code. (There are 3.)
o Identify the logic errors in the above code. (There are 2.)

A
  • no ; after first word
  • & instead of +
  • No string before secondWord
  • no “ “ between the words
69
Q

• What is the difference between the System.out.println() and System.out.print() methods?

A

println will start a new line after printing, whereas print will not.

70
Q

• What is the purpose of a compiler? a processor?

A

Compiler translates code to be executable. Processor then executes that code for a program.

71
Q

• Describe the purpose of a constructor. What is the difference between a default constructor and an explicit constructor? What return type is specified for a constructor?

A

Constructor builds an object from a class. Default is when nothing is passed & explicit is when specific data is passed through the constructor. No return type.

72
Q
•	Given the following definition statements for constructors - create a new object instance.
public Dog()
public Car(String inMake, String inColor, int inMPG)
public House(double inLatititude, double inLongitude, String inColor)
A
Dog tmpDog = new Dog();
Car tmpCar = new Car(, , );
Housse tmpHouse = new House(, , );
73
Q

• Evaluate the following statement:
myAccount.deposit(payCheck);
o Identify the object(s), method(s), parameter(s)
o How many total parameters (implicit and explicit) are there?
o Is there an implicit parameter? If yes, what is it?
o Is there an explicit parameter(s)? If yes, what is it?

A

Obj > myAccount
method > deposit
param > paycheck

1 param

no implicit

explicit, payCheck

74
Q

• What makes a valid Java identifier?

A
letters, numbers, _ &amp; $
cannot start with number
no symbols
no reserve
methods lower case
classes upper case
75
Q

What are the primitive data types in Java?

A

int, char, double, boolean, float

76
Q
•	Which variable type would be used for the following data?
GPA
name
age (in years)
batting average
football score
customer address
A
  • double
  • String
  • int
  • double
  • int
  • String
77
Q
•	Consider the following java code:
Car newCar1 = new Car("Chevy", "Blue", 32);
Car newCar2 = new Car(newCar1);

o How many objects are created?
o Are the instance variables referred to by newCar1 the same as the instance variables referred to by newCar2?

A
  • 2

- no

78
Q

• Consider the following java code:
Car newCar1 = New Car(“Chevy”, “Blue”, 32);
Car newCar2 = newCar1;

o How many objects are created?
o Are the instance variables referred to by newCar1 the same as the instance variables referred to by newCar2?

A
  • 1

- yes

79
Q

• Evaluate the following statement:
public void deposit( double inAmount)
o Is this an accessor or mutator method? How can you tell?
o Does this method return a value? If yes, what type is the return value?
o Does this method accept explicit input parameters? If yes, what type(s) is the parameter(s)?

A
  • mutator
  • no
  • Yes, a double
80
Q
•	What will be the output of the following:
System.out.print("COMP");
System.out.println("111");
System.out.print(" is a ");
System.out.println("great class.");
A

COMP111

is a great class.

81
Q
•	Reviewing each of the following variable names - do any of them violate Java syntax?  violate Java convention?
tmpStation
return
noMore? 
TmpStation
A
  • no
  • yes
  • yes
  • yes
82
Q

• Describe abstraction, encapsulation, information hiding, and black box.

A
  • encapsulation is hiding of implementation details, allows use w/o having to know it’s implementation, ie String
  • abstraction is a single concept. The basic details for an object
  • black box is any device whose inner workings are hidden. Provides encapsulation.
  • info hiding makes it easier for the implementer of a class to locate errors & change implementation.
83
Q

What is a unit test?

A

Testing in isolation, outside a computer program

84
Q

• What are javadocs? How is a javadoc created? What parameters can be specified for a javadoc?

A

Specifically formatted comments within a program. Created between /** & */. Meant to document the development of a program.

85
Q

• What is a local variable? What is the lifetime of a local variable?

A

A variable found solely within a method, and only accessible within that method. It dies once the method is exited.

86
Q

• How is an implicit parameter denoted within a class?

A

this

87
Q

• How many parameters, both implicit and explicit are in the following statements, assuming String tmpString = “Hello Class!; ?
String newString = tmpString.substr(1, 3);
String upperString = tmpString.upperCase();
boolean isEqual = tmpString.equals(“Hello Class!”);

A
  • 3
  • 1
  • 2
88
Q

• What are the basic steps for testing a method? How do you write an assert statement to test a method that returns an integer value? a double value? a string value?

A

Call the constructor, assert not null, assert equals/true/false

89
Q

• Evaluate the following statement, assuming that initially a = 10 and b = 11.
c = a + b++;
o What will a be equal to after this statement is executed?
o What will b be equal to after this statement is executed?
o What will c be equal to after this statement is executed?

A
  • 10
  • 11
  • 22
90
Q

• Describe final and static with regards to defining variables.

A
  • Final is a constant variable that will not change. Defined at the top of a Class. Prohibits alteration within program. Written in all caps & _.
  • Static means that a constant variable belongs to a specific class. Means there will be one instance of that variable.
91
Q

• Define a constant variable with a name of MINS_PER_HR set equal to the value 60.

A

private final int MINS_PER_HR = 60;

92
Q

• What is an instance variable? Should an object be manipulated by directly accessing its instance variables? What is the lifetime of an instance variable?

A

Instance variables are defined within a class. All methods are able to access instance variables with set/get. Instance variables lifetime is the entirety of the Class.

93
Q

• What will be the result of the following equation?

3 + (4 % 2) - 6 / 4

A

2

94
Q
•	Evaluate each of the following statements and determine the number type.  Will the statement successfully compile?
int tmpNum = 40;
double tmpNum = 40.0;
int tmpNum = 40.0;
double tmpNum = 40;
A
  • yes
  • yes
  • no
  • yes
95
Q

• Consider the following equation:
double battingAverage = 2.81;
int avgRounded = battingAverage;

• What value will avgRounded contain? Or will an error result?

A

Error, as it cannot compile

96
Q

• What is the first index position of a string?

A

0

97
Q

• In the .substring(int, int) method:
o What does the first explicit parameter represent?
o What does the second explicit parameter represent?
o What element position is the first element position in a string?

A
  • begin index
  • end index
  • 1
98
Q

• Using the .substring() method, write the code to do the following for the String variable testString.
o return just the first character
o return just the last character
o remove the first character from the string
o remove the last character from the string

A
  • String phrase = phrase.substring(phrase.length -1);
  • String phrase = phrase.substring(phrase.length - (phrase.length-1));
  • String phrase = phrase.substring(1);
  • String phrase = phrase.substring(phrase.length-1);