CPAN116 – Algorithms & Problem Solving – Quiz 2

Question: What is a field/attribute?

Answer: an attribute or characteristic that describes an object

 

Question: What is an object?

Answer: an instance of a class

 

Question: Pseudo code is case sensitive.

Answer: False

 

Question: Find the error in the following code snippet:
public class Car{
Scanner in = new Scanner(System.in);
public static void main(String[] args){
double mileage = 123.45;
getMileage();
System.out.println(“You’ve driven ” + mileage + “miles”);
}
static void getMileage(){
System.out.println(“Enter your vehicle’s mileage”);
mileage = in.nextDouble();
}
}

Answer: The real variable mileage is local variable and its scope is main() module only.
The module getMileage() can not access mileage in the second statement and thus cause an error.

 

Question: It is an error if you try to use a local variable without declaring it.

Answer: True

 

Question: One way to find the classes needed for an object-oriented program is to identify all of the verbs in a description of the problem domain.

Answer: False

 

Question: How do we call a module?

Answer: moduleName();

 

Question: Identify the standard diagram for graphically depicting attributes and methods that belong to a class.

Answer: UML (Unified Modelling Language) class diagram

 

Question: A module returning a calculated value must include ______________as a last statement in its body.

Answer: return

 

Question: Match the code snippets with the given description.

Answers:
int number = scanner.nextInt(); = Enter data from the keyboard and assign it to a variable
result = number + number * 3; = Solve an expression and assign the value to the result variable
return result; = A module returns a calculated value
void getInput(Scanner scanner) = Declare a module

 

Question: Select the appropriate data type for the following variables.

Answers:
Price = double
Inventory item name = String
Inventory number = int
Obsolete item (yes/no) = boolean

 

Question: Select the best answer(s) by examining the code below and determine what problem it was designed to solve.
public class Modular {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
show1();
show2();
}
static int getInput(){
System.out.println(“Enter the number”);
int num = in.nextInt();
return num;
}
static void show1(){
int number = getInput();
int result = number * number * number;
System.out.println(“Answer one is ” + result);
}
static void show2(){
int number = getInput();
int result = number * 2;
System.out.println(“Answer two is ” + result);
}
}

Answer: cube of the number and Double the number

 

Question: In an object-oriented problem/story, what words are eligible candidates to be classes and fields/attributes?

Answer: nouns

 

Question: Identify the names of all the methods.
public static void main(String[] args){
findCube(4);
findDouble(3);
}
static void findDouble(int num){
…………………….
}
static void findCube(int num){
…………………….
}
}

Answer: main, findDouble & findCube

 

Question: Select all the attributes from the following diagram:

Answer: examName, numQuestions and pointsEach

 

Question: Which tool / diagram gives a visual representation of the relationships between modules in a program?

Answer: hierarchy chart

 

Question: What module does the hierarchy chart begins with?

Answer: main

 

Question: What is true of a global variable? (Check ALL the right answers)

Answer: it can be called by any module in the algorithm AND it’s scope is the whole program

Leave a comment