java flashcards (pt.2)
Which of the following statements is true about variables?
a
A financial planner wants to calculate the average rate of return for clients. She does this by dividing the earnedIncome by the principal amount and displays the value as a double. Which of the following will correctly calculate and store the returnRate, assuming earnedIncome and principal are integers? I. double returnRate = earnedIncome / principal; II. double returnRate = (double) earnedIncome / principal; III. double returnRate = (double) (earnedIncome / principal);
b
Consider the following class: public class Coin { private String name; private double value; public Coin(String theName, double theValue) { name = theName; value = theValue; } public double getValue() { return value; } } Assume that a Coin object quarter has been properly declared and initialized. Which of the following code snippets will successfully assign the value of quarter to a new variable coinWorth?
b
A science teacher wants to create an Elements class for the Periodic Table. They want to include several attributes to the Elements class: the atomic weight, the element name, and the atomic number for the element. For example, if you wanted to create an entry for Oxygen you would use the following data:Name: OxygenAtomic Weight: 15.999Atomic Number: 8 Which of the following instance variables makes the most sense based on these attributes?
c
What is the value of x after this code runs? int x = 5; x = 10; x = 4;
c
What is true of a void method?
c
What output will be produced by System.out.println("Hello"); System.out.println("Karel");
c
What will the following code print? int n = 5; n ++; n ++; n += n; System.out.println(n);
c
Given a and b as properly initialized integers, which of the following will result in a correct calculation with a decimal answer?
d
Which of the following code segments would correctly assign word from the String sentence = "Grab the last word" to the variable lastWord? I. String lastWord = sentence.substring(sentence.length()-4, sentence.length()); II. String lastWord = sentence.substring(sentence.indexOf("w"), sentence.indexOf("d")); I II. String lastWord = sentence.substring(14, 16) + sentence.substring(1, 2) + sentence.substring(sentence.length()-1, sentence.length());
d
The code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized. /* missing code */ System.out.print(volume); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? double baseArea = pi * r * r;double volume = baseArea * h; double volume = pi * r * r;volume = volume * h; double volume = pi * r * r * h;
e
What is the result of this expression? (int) (5 + 2 / 3 + 1)
b
Which of the following arithmetic expressions evaluates to 1 ? 2 / 5 % 3 2 / (5 % 3) 2 / 5 + 1
d
What is returned by this method call: translator("pokemon")? public String translator(String word) { return word.substring(1) + word.substring(0,1) + "ay"; }
b
What is the proper syntax to declare and initialize a variable called temperature to have the value 70.4?
b
What is wrong with this method definition? public int printPayAmount(int amount) { System.out.println(amount); }
b
What will the value of myBankAccount be after the method call depositMoney(myBankAccount, 572);? int myBankAccount = 122; public void depositMoney(int bankAccount, int deposit) { bankAccount += deposit; }
b
Which statement correctly declares a variable that can store a temperature rounded to the nearest tenth of a degree?
b
Assume that a, b, and c are all integers with values of 90, 5, and 4 respectively. What would the value of x be in this expression? int x = a / b / c;
c
Consider the following code segment. System.out.println("W"); System.out.println("X"); System.out.print("Y"); System.out.print("Z"); What is printed as a result of executing the code segment?
W X YZ
A coffee shop has created a DrinkOrder class. The class contains variables to represent the following: A String variable called name to represent the name of the drink. An int variable called ounces to indicate how many ounces the drink should be. A boolean variable called isIced to indicate whether the drink is iced. An object Latte has been declared as type DrinkOrder after someone has ordered a Latte. Based on the information provided, which would be the most accurate class declaration for the DrinkOrder class?
a
A procedure that is defined by the user is called a
a
After the execution of the following lines of code, what will be output onto the console? String letters = "ABCde"; String name = "Karel the Dog"; String letter = "D"; System.out.println(name.indexOf(letter)); System.out.println(letters.indexOf(name.substring(3,4))); System.out.println(letters.indexOf(letter));
a
Consider the following code segment. System.out.print("Ready"); // Line 1 System.out.print("Set"); // Line 2 System.out.print("Go!"); // Line 3 The code segment is intended to produce the following output but may not work as intended. Ready Set Go! Which change, if any, can be made so that the code segment produces the intended output?
a
Each of the following code segments is intended to print the word Hello. Which of the following code segments works as intended? System.out.print("Hello"); System.out.print(Hello); System.out.print(He);System.out.print(llo);
a
Given the following, what will be printed out? int a = 2; int b = 3; int c = 4; System.out.println(a * b + b / a + (a * c / 4.0) * c);
a
What is the result of the following expression when x is 125? x % 6
a
What will the output of the following lines of code be? int x = 10; int y = x / 4; System.out.print("x + y = "); System.out.print(x + y);
a
Which expression returns the 1's place of an integer x?
a
Which of the following code segments would successfully square the minimum value between two int variables x, y?
a
Which of the following print: Hello Java! I. System.out.println("Hello Java!"); II. System.out.print("Hello Java!"); III. System.out.print("Hello"); System.out.print("Java!"); IV. System.out.println("Hello"); System.out.println("Java!");
a
Consider the following class: public class Insect { private String name; private int numLegs; private boolean hasWings; private boolean hasExoskeleton; public Insect(String theName, int legNumber, boolean isWinged, boolean isExoskeleton) { name = theName; numLegs = legNumber; hasWings = isWinged; hasExoskeleton = isExoskeleton; } public Insect(String theName, int legNumber, boolean isWinged) { name = theName; numLegs = legNumber; hasWings = isWinged; hasExoskeleton = true; } } Which of the following is NOT a possible header for a new constructor for the Insect class?
b
Consider the following code segment. System.out.print("cat "); System.out.println("dog "); System.out.println("horse "); System.out.print("cow "); What is printed as a result of executing the code segment?
b
Consider the following code segment. int x = 10; int y = 20; /* missing code */ System.out.print(top / bottom); Which of the following replacements for /* missing code */ will cause an ArithmeticException to occur? int top = x - y;int bottom = y - x; int top = 2 * x;int bottom = y - top; int top = x + y;int bottom = 2 * top;
b
Consider the following method: public double doubleVal(double x) { return x *2; } The following code segment calls the method doubleVal: Double val = 6.5; System.out.println(doubleVal(val)); What is printed when the code segment is executed?
b
Joe's Pizza is creating a program that will calculate the total amount of money an employee earns each week. Employees are paid by the hour and only work in 1 hour increments. Salaries start at minimum wage, but employees get a $0.50 raise after the first month. Which variables would be the best to store the hours and salary of the employees?
b
Refer to the following code segment: double myDouble = 1/4; System.out.println("1 / 4 = " + myDouble); The output of the code is: 1 / 4 = 0.0 The student wanted the output to be: 1 / 4 = 0.25 Which change to the first line of their code segment would get the student the answer that they wanted?
b
The following code is intended to print 8. int x = 23; double y = 3; System.out.println((int)(x / y)); What is printed and why?
b
A teacher determines student percentages in a course as the points a student earns divided by the total points available in the grading period. Points are awarded only in whole number increments, but student percentages are to be stored as decimals. The following code segment appears in a program used to compute student percentages. Points that a student earns are stored in pointsEarned, the total points available in the grading period are stored in totalPoints, and the student percentage is stored in percentage. int pointsEarned; /* missing code */ Which of the following is most appropriate to replace /* missing code */ in the program?
c
Consider the following code segment. int a = 1; int b = 2; int c = 3; int d = 4; double x = a + b * c % d; What is the value of x when the code segment has been executed?
c
Consider the following code segment: String str = "I am"; str += 10 + 3; String age = "years old"; System.out.println(str + age); What is printed as a result of executing the code segment?
c
How many lines will be printed with the following statement? System.out.println("Hello"); System.out.println(" World"); System.out.print("Welcome to"); System.out.print("Java.");
c
What are parameters?
c
What does this method call doubleNumber(7.8); return, given the following method? public double doubleNumber(double myNumber) { return (double) (int) myNumber * 2; }
c
What is the value of myInteger after this line of code is executed? int myInteger = (int) 5.6;
c
What would the output be of the following code segment: RandomCalculator calc = new RandomCalculator(4, 5); System.out.println(calc.add(5.0)); System.out.println(calc.add(5.0, 5)); System.out.println(calc.add(5, 5.0));
c
Which of the following is not a primitive type?
c
A coffee shop has created a DrinkOrder class. The class contains variables to represent the following: A String variable called name to represent the name of the drink. An int variable called ounces to indicate how many ounces the drink should be. A boolean variable called isIced to indicate whether the drink is iced. An object latte has been declared as type DrinkOrder after someone has ordered a Latte. Which of the following descriptions is accurate?
d
A teacher has calculated the gradeAverage as a double, but for report cards, she needs to report it rounded to the nearest whole number. Assuming that we round up from 0.5, which of the following will correctly round the gradeAverage?
d
All non-void methods must have which of the following?
d
Assume y is a properly initialized positive integer. Which of the following will always result in a value of 1?
d
Consider the code segment below. int x = 10; int y = 20; System.out.print(y + x / y); What is printed as a result of executing the code segment?
d
Consider the following class: public class Dog { private String name; private String breed; public String getName() { return name; } } An object Karel is created using the Dog class. What would the result of the command System.out.println(Karel.getName()); be?
d
Consider the following class: public class RaffleTicket { private String ownerName; private int ticketNum; public RaffleTicket(String name) { ownerName = name; ticketNum = getTicketNum(); } private int getTicketNum() { /* Code to be implemented */ } Which of the following can be used to replace /*Code to be implemented */ so that the code segment produces a random number between 1-100?
d
Consider the following code segment. System.out.print("Hello!"); System.out.println("How "); System.out.print("are "); System.out.print("you?"); What is printed as a result of executing the code segment?
d
Consider the following code segment. int x; int y; x = 3; y = /* missing expression */; x = 1 + 2 * y; System.out.print(x); System.out.println(y); Which of the following can be used as a replacement for /* missing expression */ so that the code segment prints 94 ? Responses
d
Consider the following code segment: /* data type 1 */ x = 0.5; /* data type 2 */ y = true; Which of the following best describes the data types that should be used to replace/* data type 1 */ and /* data type 2 */ so that the code segment compiles without error?
d
What does the method call mystery("APCSA"); return for the following method? public String mystery(String x) { return x * 3; }
d
What is the result of this expression? 4 + 8 * 3 / 4 + 5 % 2
d
What will this method call print to the screen? public static void formatText(int a, int b, String c) { System.out.println(b + " " + c + ", " + a); } formatText(2018, 17, "Dec")
d
Which of the following code segments will produce this output in the console: "Hello" "Hola" I. Greetings hi = new Greetings("Hello"); hi.translate(); hi.greeting(); II. Greetings.hello(); Greetings.translate(); Greetings.greeting(); III. Greetings hello = new Greetings(); hello.hello(); hello.translate(); hello.greeting(); IV Greetings hola = new Greetings("Hello"); hola.changeGreeting("Hola"); hola.greeting();
d
Which of the following values can correctly be saved in a boolean?
d
Which of the following would equal 2? I. int x = 0; x ++; x += x; II. int y = 4; y ++; y /= 2; III. int z = 4; z += 2; z /= 2;
d
Why do we use methods in Java? I. To make code easier to understand II. To define global variables III. To avoid repeated code IV. To simplify code V. To avoid needing to define them as public or private
d
Consider the following code segment. System.out.println(hello); // Line 1 System.out.print(world); // Line 2 The code segment is intended to produce the following output but does not work as intended. hello world Which of the following changes can be made so that the code segment produces the intended output?
e