Programming Flashcards
The programmer, not the compiler, is responsible for testing a program to identify _________________.
run-time errors
An integrated development environment (IDE) bundles tools for programming into a unified application. What kinds of tools are usually included?
an editor and a compiler
How do programmers find exceptions and run-time errors?
Testing by running the program with a variety of input values
High-level programming languages ________________________
are independent of the underlying hardware
When a program begins to run, ___________________________.
it is moved to the CPU’s memory
Which of the following typically provides data persistence without electricity?
I. The CPU’s memory
II. The hard disk
III. Secondary storage
II and III only
An example of an output device that interfaces between computers and humans is ____________.
a speaker
Which one of the following translates high-level descriptions into machine code?
compiler
What is the difference between an editor and a compiler?
An editor allows program files to be written and stored; a compiler converts program files into an executable program.
Programs that are not running are usually stored _____________________.
in secondary storage
Consider the following statements regarding computers.
I. Computers can execute a large number of instructions in a fraction of a second.
II. Computer application areas mainly target the research and scientific communities.
III. The physical components of a computer constitute its hardware.
IV. Unlike humans, a computer never gets bored or exhausted when performing repetitive tasks.
I, III, and IV only
A sequence of steps that is unambiguous, executable, and terminating is called _______________.
an algorithm
Who or what is responsible for inspecting and testing the program to guard against logic errors?
programmer
Computer programming is ______________________.
the act of designing and implementing a computer program
Computers are machines that __________________.
execute programs
An example of an input device that interfaces between computers and humans is ____________.
a microphone
Computer scientists have devised __________________ that allow programmers to describe tasks in words that are closer to the syntax of the problems being solved.
high-level programming languages
While developing a program, the programmer adds the discount amount to the total due instead of subtracting it. What type of an error is this?
logic error
The Central Processing Unit is primarily responsible for ______________.
performing program control and data processing
Which of the following guidelines will make code more explanatory for others?
Add comments to source code.
Assuming that the user inputs a value of 30 for the price and 10 for the discount rate in the following code snippet, what is the output?
Scanner in = new Scanner(System.in);
System.out.print(“Enter the price: “);
double price = in.nextDouble();
System.out.print(“Enter the discount rate: “);
double discount = in.nextDouble();
System.out.print(“The new price is “);
System.out.println(price - price * (discount / 100.0));
The new price is 27.0
Assuming that the user inputs “Joe” at the prompt, what is the output of the following code snippet?
public static void main(String[] args)
{
System.out.print(“Enter your name “);
String name;
Scanner in = new Scanner(System.in);
name = in.next();
name += “, Good morning”;
System.out.print(name);
}
Joe, Good morning
Which one of the following code snippets compiles without errors and displays the output “Hello Good Day!” on the screen?
System.out.print(“Hello “);
System.out.println(“Good Day!”);
Assuming that the user inputs a value of 25000 for the pay and 10 for the bonus rate in the following code snippet, what is the output?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter the pay: “);
double pay = in.nextDouble();
System.out.print(“Enter the bonus rate: “);
double bonus = in.nextDouble();
System.out.println(“The new pay is “ +
(pay + pay * (bonus / 100.0)));
}
The new pay is 27500
What is the output of the following code snippet?
System.out.print(“Hello”);
System.out.println(“Good Day!”);
HelloGood Day!
At what point in the problem-solving process should one write pseudocode?
Before writing Java code, as a guide for a general solution
What is the output of the following code snippet?
public class PrintIt
{
public static void main(String[] args)
{
System.out.println(“4 * 4” + 12);
}
}
1612
Which of the following statements with comments is(are) valid?
I. int cnt = 0; /* Set count to 0
II. int cnt = 0; /* Set count to 0 */
III. int cnt = 0; // Set count to 0
II and III only
Assuming that the user inputs a value of 30 for the price and 10 for the discount rate in the following code snippet, what is the output?
Scanner in = new Scanner(System.in);
System.out.print(“Enter the price: “);
double price = in.nextDouble();
System.out.print(“Enter the discount rate: “);
double discount = in.nextDouble();
System.out.print(“The new price is “);
System.out.println(price - price * (discount / 100.0));
The new price is 27.0
What does the following code do?
int sum = 0;
final int count = 1000;
for (int i = 1; i <= count; i++)
{
sum = sum + (int) (Math.random() * 101);
}
System.out.println(sum / count);
It calculates the average of 1000 random integers between 0 and 100.
Which of the following code snippets will generate a random number between 0 and 79?
int val = (int) Math.random() * 80;
Assuming that the user enters 23 and 45 as inputs for num1 and num2, respectively, what is the output of the following code snippet?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter a number: “);
String num1 = in.next();
System.out.print(“Enter another number: “);
String num2 = in.next();
System.out.println(num1 + num2);
}
2345
Which one of the following operators computes the remainder of an integer division?
%
Suppose that the chance to hit the jackpot in a lottery is one in one million. Which of the following code snippets simulates that random number?
(long) (Math.random() * 1000000 + 1)
What does the following statement sequence print if the user input is 123?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter a number “);
int myInt = in.nextInt();
myInt += 456;
System.out.println(myInt);
}
579
What does the following code do?
int sum = 0;
final int count = 1000;
for (int i = 1; i <= count; i++)
{
sum = sum + (int) (Math.random() * 101);
}
System.out.println(sum / count);
It calculates the average of 1000 random integers between 0 and 100.
Which operator is used to concatenate two or more strings?
+
What is the difference between the result of the following two Java statements?
I. int cents = (int)(100 * price + 0.5);
II. int cents = (100 * price + 0.5);
Statement I compiles, but II does not
What is the output of the following code snippet?
public static void main(String[] args)
{
int s;
double f = 365.25;
s = f / 10;
System.out.println(s);
}
No output because the code snippet generates compilation errors.
What does the following statement sequence print?
String str = “Java”;
str += “ is powerful”;
System.out.println(str);
Java is powerful
Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the following code snippet, what is the output?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter the price: “);
double price = in.nextDouble();
System.out.print(“Enter the discount rate: “);
double discount = in.nextDouble();
System.out.println(“The new price is “ +
price - price * (discount / 100.0));
}
The new price is 22.5
Imagine you are planning to buy a new cell phone. You are considering two cell phones. These cell phones have different purchase prices. Each mobile service provider charges a different rate for each minute that the cell phone is used. To determine which cell phone is the better buy, you need to develop an algorithm to calculate the total cost of purchasing and using each cell phone. What are all the inputs needed for this algorithm?
The cost of each cell phone, the rate per minute for each cell phone, and the number of minutes you would use the cell phone
Which Java statement prints a blank line?
System.out.println();
What is the value inside the value variable at the end of the given code snippet?
public static void main(String[] args)
{
int value = 3;
value = value – 2 * value;
value++;
}
–2
What is the output of the following code snippet?
System.out.print(4 + 4);
System.out.print(12);
812
What does the following statement sequence print if the user input is 123?
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter a number: “);
String str = in.next();
str += 456;
System.out.println(str);
}
123456
Which of the following is correct for simulating the toss of a pair of coins to get 0 (head) or 1 (tail) with different probabilities?
(int) (Math.random() * 2) + (int) (Math.random() * 2)
The Math.ceil method in the Java standard library takes a single value x and returns the smallest integer that is greater than or equal to x. Which of the following is true about Math.ceil(56.75)?
The argument is 56.75, and the return value is 57.
Which of the following code snippets can be used for defining a method that does not return a value?
The method should accept a string and then display the string followed by “And that’s all folks!” on a separate line.
public void displayMessage(String str)
{
System.out.println(str);
System.out.println(“And that’s all folks!”);
}
In a vehicle mileage application, you enter number of miles traveled by a vehicle and the amount of fuel used for 30 consecutive days. From this data the average monthly mileage of the vehicle is calculated. Which of the following should be done to improve the program design?
Consider writing a method that returns the average monthly mileage as a double value.
An effective technique for understanding the subtle aspects of a method is to _____________.
perform a manual walkthrough.
Which of the following code snippets can be used for defining a method that does not return a value?
The method should accept a string and then display the string followed by “Just Let’s learn Java!” on a separate line.
public void showString(String someString)
{
System.out.println(someString);
System.out.println(“Just Let’s learn Java!”);
}
Which of the following is the best choice for a return type from a method that prompts users to enter their credit card number exactly as it appears on the card?
String
The term “Black Box” is used with methods because
Only the specification matters; the implementation is not important.
When hand-tracing methods, the values for the parameter variables ______________
are determined by the arguments supplied in the code that invokes the method
What is the output of the following code snippet, assuming the class name is Example?
public void doubleAmount(int intvalue)
{
intvalue = 2 * intvalue;
}
public static void main(String[] args)
{
Example tester = new Example();
int principalAmt = 2000;
System.out.println(tester.doubleAmount(principalAmt));
}
Compilation error
The purpose of a method that returns void is __________________.
to package a repeated task as a method even though the task does not yield a value
What is the syntax error in the following method definition?
public String parameter(double r)
{
double result;
result = 2 * 3.14 * r;
return result;
}
The value that is returned does not match the specified return type.
Which of the following is the correct header for a greaterThan method definition that takes two arguments of type double and returns true if the first value is greater than the second value?
public boolean greaterThan(double a, double b)
What is stepwise refinement?
The process of breaking complex problems down into smaller, manageable steps
Which of the following is the correct header for a nonstatic method definition named calcSum that accepts four int arguments and returns a double?
public double calcSum(int a, int b, int c, int d)
What is the problem with the definition of the following method that calculates and returns the tax due on a purchase amount?
public double taxDue(double amount, double taxRate)
{
double taxDue = 0.0;
taxDue = amount * taxRate;
}
The taxDue method does not return a value.
What is the problem with the code snippet below?
(Assume that a method-calling noun of the class named “tester” exists)
public String val()
{
String result = “candy”;
return;
}
. . .
// Using method val()
System.out.println(“The value is: “ + tester.val());
The method val does not have a return value.
Which of the following options describes the process of stepwise refinement?
I. Using arguments to pass information to a method
II. Using unit tests to test the behavior of methods
III. Decomposing complex tasks into simpler ones
III only
What can be used as an argument in a method call?
I. A variable
II. An expression
III. Another method call that returns a value
IV. Another method call that has no return value
I, II and III only
What is the syntax error in the following method definition?
public String parameter(double r)
{
double result;
result = 2 * 3.14 * r;
return result;
}
The value that is returned does not match the specified return type.
You need to write a method that calculates the volume for a shape, which depends on the shape’s length, width, and height. What should be the parameter variables and their data types for this method?
double width, double length, double height
What is the problem with the definition of the following method that calculates and returns the tax due on a purchase amount?
public double taxDue(double amount, double taxRate)
{
double taxDue = 0.0;
taxDue = amount * taxRate;
}
The taxDue method does not return a value.
Which of the following is NOT a good practice when developing a computer program?
Put as many statements as possible into the main method.
For a program that reads city names repeatedly from the user and calculates the distance from a company’s headquarters, which of the following would be a good design based on stepwise refinement?
Write one method that reads city name and another method that calculates distance.
If a method is declared to return void, then which statement below is true?
When the method terminates no value will be returned.
Which of the following is not legal in a method definition?
Multiple return values
What step should you take after implementing a method?
Test the method in isolation.
What is the output of the following code snippet?
Assume that the class is named Example.
public int doIt(int a, int prv1, int nxt1)
{
int prv = a - prv1;
int nxt = a + nxt1;
return prv;
}
public static void main(String[] args)
{
int a = 100;
int b = 100;
int c = 100;
Example tester = new Example();
b = tester.doIt(a, b, c);
System.out.println(“b = “ + b + “, c = “ + c);
}
b = 0, c = 100
Which of the following code snippets can be used for defining a method that does not return a value?
The method should accept a string and then display the string followed by “Just Let’s learn Java!” on a separate line.
public void showString(String someString)
{
System.out.println(someString);
System.out.println(“Just Let’s learn Java!”);
}
What is the syntax error in the following method definition?
public String parameter(double r)
{
double result;
result = 2 * 3.14 * r;
return result;
}
The value that is returned does not match the specified return type.
What is the output of the following code snippet?
Assume that the class name is Example.
public int assignPriority(int priority)
{
return priority + 2;
}
public static void main(String[] args)
{
Example tester = new Example();
int priority = tester.assignPriority(3);
System.out.println(“Priority: “ + priority);
}
Priority: 5
Given the following method, what is the result of calling the method, assuming a method calling noun of the class named “tester” exists, as tester.getNumber(n)?
public double getNumber(double n)
{
return Math.pow(Math.sqrt(n), 2) - n;
}
Close to 0, but not always 0