Card 0 of 693
Consider the following C++ pseudocode:
Class Car {
const int wheels = 4;
float milesPerGallon;
string make;
string model;
}
Car sportscar = new Car;
What is the difference between the class car
, and the object sportscar
?
When programming within the Object-Oriented paradigm, think of the class as a blueprint, and the object as a house built from that blueprint.
The class Car
is a abstract specification that does not refer to any one particular instance. It is merely a protocol that all objects wishing to be cars should follow. The sportscar object is a realization of the car blueprint. It is a specific instance. In programming jargon, "sportscar
is instantiated from Car
."
Compare your answer with the correct one above
Consider the following JAVA code:
**public static void main(String[] args)**
**{**
**double number1 = 99.05;**
**int number2 = (int) number1;**
**System.out.println("number1 = " + number1);**
****System.out.println("number2 = " + number2);**
**}**
What would be the console output?
Type casting deals with assigning the value of a variable to another variable that is of a different type. In this case we have two variables one that is a double, and another that is an integer. number1 is a double that has a value of 99.05. However, when number2 is assigned the value of number1, there is some explicit type casting going on. When an integer is assigned the value of a double, it drops off the decimal places. This means that number2 has a value of 99.
Compare your answer with the correct one above
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
Compare your answer with the correct one above
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
Compare your answer with the correct one above
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String \[\] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
Compare your answer with the correct one above
Why is the "default" keyword used in a switch statement?
The default keyword is used to catch any remaining cases not specified. For example, if you only want to check for certain hour of the day, say 2,3, and 4 o'clock, the default case would catch all the other hours. You could then create a statement for what to do when those values are caught.
Compare your answer with the correct one above
True or False.
This code snippet will iterate 5 times.
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("string0");
arrList.add("string1");
arrList.add("string2");
arrList.add("string3");
arrList.add("string4");
for (int i = 0; i < arrList.size(); i++) {
System.out.println(arrList.get(i));
}
The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.
Compare your answer with the correct one above
Suppose you have the following code:
public static void main(String\[\] args) {
int a =2;
if (a%2==0)
System.out.println("Hello World");
else
System.out.println("Hi");
}
If the main method is called, what will be printed?
"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.
Compare your answer with the correct one above
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
Compare your answer with the correct one above
int x=2;
double y=2.1;
float z=3.0;
int c=(x*y) + z;
What is the value of c
Remember that if a variable is declared as an integer, it can't have any decimals.
int c=(x*y) + z;
int c=(2*2.1)+3.0
From here, we do our math operations to solve, remembering to use the correct order of operations. Thus, start with the parentheses first then do the addition.
int c=4.2+3
int c= 4+3
int c=7
Any leftover fractions are cut off, so the answer is shortened to just 7.
Compare your answer with the correct one above
Consider the following C++ pseudocode:
Class Car {
const int wheels = 4;
float milesPerGallon;
string make;
string model;
}
Car sportscar = new Car;
What is the difference between the class car
, and the object sportscar
?
When programming within the Object-Oriented paradigm, think of the class as a blueprint, and the object as a house built from that blueprint.
The class Car
is a abstract specification that does not refer to any one particular instance. It is merely a protocol that all objects wishing to be cars should follow. The sportscar object is a realization of the car blueprint. It is a specific instance. In programming jargon, "sportscar
is instantiated from Car
."
Compare your answer with the correct one above
Consider the following JAVA code:
**public static void main(String[] args)**
**{**
**double number1 = 99.05;**
**int number2 = (int) number1;**
**System.out.println("number1 = " + number1);**
****System.out.println("number2 = " + number2);**
**}**
What would be the console output?
Type casting deals with assigning the value of a variable to another variable that is of a different type. In this case we have two variables one that is a double, and another that is an integer. number1 is a double that has a value of 99.05. However, when number2 is assigned the value of number1, there is some explicit type casting going on. When an integer is assigned the value of a double, it drops off the decimal places. This means that number2 has a value of 99.
Compare your answer with the correct one above
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
Compare your answer with the correct one above
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
Compare your answer with the correct one above
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String \[\] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
Compare your answer with the correct one above
Why is the "default" keyword used in a switch statement?
The default keyword is used to catch any remaining cases not specified. For example, if you only want to check for certain hour of the day, say 2,3, and 4 o'clock, the default case would catch all the other hours. You could then create a statement for what to do when those values are caught.
Compare your answer with the correct one above
True or False.
This code snippet will iterate 5 times.
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("string0");
arrList.add("string1");
arrList.add("string2");
arrList.add("string3");
arrList.add("string4");
for (int i = 0; i < arrList.size(); i++) {
System.out.println(arrList.get(i));
}
The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.
Compare your answer with the correct one above
Suppose you have the following code:
public static void main(String\[\] args) {
int a =2;
if (a%2==0)
System.out.println("Hello World");
else
System.out.println("Hi");
}
If the main method is called, what will be printed?
"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.
Compare your answer with the correct one above
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
Compare your answer with the correct one above
int x=2;
double y=2.1;
float z=3.0;
int c=(x*y) + z;
What is the value of c
Remember that if a variable is declared as an integer, it can't have any decimals.
int c=(x*y) + z;
int c=(2*2.1)+3.0
From here, we do our math operations to solve, remembering to use the correct order of operations. Thus, start with the parentheses first then do the addition.
int c=4.2+3
int c= 4+3
int c=7
Any leftover fractions are cut off, so the answer is shortened to just 7.
Compare your answer with the correct one above