Computer Programming Final Tynes
Consider the following class: public class Rectangle { private int width; private int height; public Rectangle(int rectWidth, int rectHeight) { width = rectWidth; height = rectHeight; } public int getArea() { return width * height; } public int getHeight() { return height; } public int getWidth() { return width; } public String toString() { return "Rectangle with width: " + width + " and height: " + height; } } If a new variable Rectangle shape = new Rectangle(10, 20); was initialized, what is the correct syntax for retrieving the area of shape? shape.getArea(); int area = Rectangle.getArea(); Rectangle.getHeight() * Rectangle.getWidth(); shape.getArea(10,20);
shape.getArea(10,20);
What would this program print? double sideLength = Math.sqrt(64); double height = Math.pow(3, 2); double difference = Math.abs(sideLength - height); System.out.println(difference); -1.0 0.0 8.0 1.0
1.0
Evaluate these two expressions: (int)(9 / 2 * 3) and (double)(9 / 2 * 3) 12 and 12.0 13 and 13.5 13 and 13.0 12 and 13.5
12 and 12.0
How many times will the following loop be executed? int x; for(x = 0; x <= 15; x++){ System.out.println("far out ");} 0 14 infinite 16 15
16
What is the output of the following code snippet? String forest = "Amazon Rainforest"; System.out.println(forest.indexOf('a')); System.out.println(forest.indexOf('g')); System.out.println(forest.indexOf('n')); 0 -1 5 2 -1 5 This will throw an error because 'g' is not in the string. 1 0 6
2 -1 5
How many times will Hello be printed? for(x = 1; x <= 30; x++){ for(x = 1; x <= 7; x++) { System.out.println("Hello"); }} 7 210 37 30
210
What will be assigned to num3 given the following declarations? int tot = 94; int amt = 5; int num3 = tot % amt; 4 18 18.8 0.8
4
After execution of the following line of code, what will be the value of num3 ? double num3 = 15 / 3; 0 error: incompatible types 5 5.0
5.0
What is the output? public void yourMethod(int a){ int amt = a + 2; System.out.println(amt + " apples");} public void myMethod(int x){ int num4 = x * 2; System.out.println(num4 + " birds");} public void main(String[] args){ int num1 = 8, num2 = 3, num3 = 5; myMethod(num2); System.out.println(num1 + " cats"); yourMethod(num3);} 8 cats7 apples6 birds 8 cats6 birds7 apples 6 birds 7 apples8 cats 6 birds8 cats7 apples
6 birds8 cats7 apples
What is the output? public int calculate(int a, int b){ int num1 = a - 4; int num2 = b - 8; int num3 = num1 * num2; return num3;} public void main(String[] args){ int amt = 7, num = 10; int x = calculate(amt, num); System.out.println(amt + " fries"); System.out.println(x + " shakes");} 10 fries7 shakes 7 fries6 shakes 6 fries7 shakes 7 fries10 shakes
7 fries6 shakes
Java automatically converts between objects and primitives in the process of autoboxing and unboxing. What happens when a Double is unboxed? A Double is unboxed when it is passed to a method. A Double is unboxed when it is converted to an Integer A Double is unboxed when it is converted to a Double value. A Double is unboxed when it is converted to a primitive value.
A Double is unboxed when it is converted to a primitive value.
Java automatically converts between objects and primitives in the process of autoboxing and unboxing. What happens when an int is autoboxed? A int is autoboxed when it is converted to a Double A int is autoboxed when it is converted to a primitive value. A int is autoboxed when it is converted to a Integer A int is autoboxed when it is passed to a method.
A int is autoboxed when it is converted to a Integer
What would be printed by the following code snippet? String lastName = "Vu"; String otherLastName = "Lopez"; int comparison = lastName.compareTo(otherLastName); System.out.println(comparison); A negative number because "Vu" comes before "Lopez" in lexicographical order. Zero because both strings start with capital letters A positive number because "Vu" comes before "Lopez" in lexicographical order. A negative number because "Vu" comes after "Lopez" in lexicographical order. A positive number because "Vu" comes after "Lopez" in lexicographical order.
A positive number because "Vu" comes after "Lopez" in lexicographical order.
Given: int num = 16;int amt = 3; What is the return value for each method call? A. Math.pow(3, amt) B. Math.sqrt(num) C. Math.abs(amt - num)
A. 27.0 B. 4.0 C. 13
Given: boolean A = true;boolean B = true;boolean C = false;boolean D = false; What are the values of each of the following expressions? A. A && C B. B || D C. A || D && C D. !B || !(A && C)
A. false B. true C. true D. true
Given: String school = "Xavier Prep"; What is the return value for each method call? A. school.substring(8) B. school.length() C. school.indexOf("e") D. school.substring(2, 5)
A. rep B. 11 C. 4 D. vie
Suppose a program is a client of the Player class. Here is a snippet of code contained in the program Player firstPlayer = new Player("Karel", "Warrior", "Mote Prime", 90); Looking at the documentation of the class, you find the signature for the constructor, shown below. Player Player(String name, String role, String location, int health); Where would you find the formal parameters? In the documentation In the library Both the program and the documentation contain formal parameters In the program
In the documentation
Given the method header: public void costOrder(int a) Which of the following statements about the costOrder() method is accurate? It has no return value. The data type of its return value is an int. Its method call will pass a double. The data type of its return value is a double.
It has no return value.
What is the output for the following code? System.out.println("LM"); System.out.print(245); System.out.println("CB"); System.out.print(789);
LM 245CB 789
What are the three parts of a loop? Loop control variable declared, tested, & updated Loop control variable initialized, tested, & updated Loop control variable initialized, updated, & called Loop control variable declared, assigned, & called
Loop control variable initialized, tested, & updated
Which of the following is not a primitive data type? boolean String double int
String
Which line of code has an error that will be caught by the compiler? public class TaxCalculation System.out.print("How are You?"); double cost = 4.23; String str = Sea World; public static void main(String[] args)
String str = Sea World;
What is the output? int x;int a = 2; for(x = 1; x < 4; x++){ a = a + x; }System.out.println("a: " + a + ", x: " + x); a: 8, x: 4 a: 8, x: 3 a: 3, x: 1a: 5, x: 2a: 8, x: 3a: 8, x: 4 a: 3, x: 1a: 5, x: 2a: 8, x: 3
a: 8, x: 4
Which line of code below is used to obtain the user's age given the following two lines of code and assuming the variable, age, has already been declared? Scanner scan = new Scanner(System.in);System.out.println("Enter your age."); age = scan.nextLine(); age = scan.next(); scan.nextInt(); Correct! age = scan.nextInt();
age = scan.nextInt();
What will be the output? int num2 = 20; if(num2 >= 20) System.out.println("apple");else if(num2 > 15) System.out.println("pear");else System.out.println("peach"); applepearpeach apple applepeach applepear
apple
Tracing code without indentation and curly braces. What will be the output? int num1 = 15;int amt = 52;int max = 15;int min = 5; if(max > num1)System.out.println("car");System.out.println("boat");if(min < amt)System.out.println("truck");elseSystem.out.println("motorcycle");System.out.println("van"); boattruck carboattruckvan carboattruck boattruckvan
boattruckvan
What will be the output? int num1 = 15; if(num1 <= 10) System.out.println("bear");if(num1 < 20) System.out.println("cat");if(num1 < 30) System.out.println("dog"); bearcatdog catdog cat bearcat
catdog
Which of the following uses proper syntax to declare and initialize a memory location to store the average miles per hour to the nearest tenth? int mph = 14.1; double mph; mph = 35.4; double mph = 25.3;
double mph = 25.3;
Which value can be stored in a boolean variable? False "false" "False" false
false
How should the following line of code be fixed? if(favPetName = "Fluffy") if(Fluffy.equals(favPetName)) if(favPetName.equals("Fluffy")) if(favPetName == "Fluffy") if("Fluffy".equals("favPetName")) if(favPetName.equals(Fluffy))
if(favPetName.equals("Fluffy"))
Suppose there is a class called Student. One of the methods is given below. It sets the instance variable isHonors to the parameter value. public void setHonorStatus(boolean status) { isHonors = status; } Using the Student object called karel, which of the following is the correct way to set karel's honor status to true? karel.setHonorStatus(true); karel.setHonorStatus(status=true); karel.isHonors = true; karel.setHonorStatus(isHonors = true);
karel.setHonorStatus(true);
race the code snippet below and indicate which line produces the first error from the compiler. 1 String name = "Jane";2 int numGames = 12;3 double numPoints;4 double ptsPerGame = numGames / numPoints;5 System.out.println(name + " scored " + ptsPerGame + " points per game."); line 1 line 2 line 5 line 4 line 3
line 4
tripleNum(amt); is a(n) ____ that passes one ____ to the tripleNum() method. method call; variable method call; argument or parameter variable call; argument or parameter argument or parameter; variable call
method call; argument or parameter
What will the following code print? int num = 7, amt = 3; num *= 2;num++; amt = num; amt += num / amt; System.out.println("num is " + num + "; amt is " + amt); num is 15; amt is 16 num is 3; amt is 2 num is 15; amt is 2 num is 3; amt is 4
num is 15; amt is 16
Which line of code will result in an error message if: num1 is an int num2 is a double num3 is an int num4 is a double num1 = num1 / num3; num3 = num1 + num2; num2 = num3 / num4; num2 = num3 + num1;
num3 = num1 + num2
Given: String schMascot = "Gators"; String schCity = "Phoenix"; What is the return value of the following method call? schCity.substring(schMascot.indexOf("t"), schMascot.length()) oeni oenix enix eni oen
oeni
Match the following method call with the appropriate header of the called method. double var = someMethod(7); public double someMethod(int a) public double someMethod(double a) public void someMethod(int a) public int someMethod(int a)
public double someMethod(int a)
Consider this code snippet that uses a class called Rectangle. int roomHeight = 40; int roomWidth = roomHeight * 3; Rectangle room = new Rectangle(roomHeight, roomWidth); Java Which of the following is a reference variable? Rectangle room roomWidth roomHeight
room
What will be the output? int amt = 52;int max = 15;int min = 5; if(amt > min){ System.out.println("train");}if(amt < max){ System.out.println("boat");}else{ System.out.println("plane");}System.out.println("bike"); trainboatbike trainbike trainplane train trainplanebike
trainplanebike
Which of the following expressions returns a random integer from 1 to 25? (int)(Math.random() * 1 + 25) Math.random() * 25 + 1 Math.random() * 1 + 25 Math.random(25) * 1) (int)(Math.random(25) * 1) (int)(Math.random() * 25 + 1)
(int)(Math.random() * 25 + 1)
The purpose of a wrapper class is to Allow primitive to be passed to methods "Wrap" a primitive value to convert it to an object "Wrap" an object to convert it to a primitive value Allow methods to be called on a primitive value
"Wrap" a primitive value to convert it to an object
How many times will the following loop be executed? int x = 1; while(x < 9){ System.out.println("far out"); x++;} 8 10 9 infinite 0
8
Which of these is an example of calling a static method? square(x) point.setX(x) student.getName() Math.abs(x)
Math.abs(x)
Which three snippets of code produce an error? if(x != 10)else if(x > 10) if(x >= 30); if(x <= 10)
if(x >= 30);
Consider the following code snippet. What would the output be? String school = "Rydell High School"; System.out.println(school.substring(8)); System.out.println(school); igh SchoolRydell High School gh SchoolRydell High Schoo gh School gh School i i igh School igh School
igh SchoolRydell High School
What are two ways data can be passed from one method to another? method call and variable call variable declaration and print statement return statement and variable call method call and return statement
method call and return statement
Match the following method call with the appropriate header of the called method. someMethod(7, 10.4, "Billy"); public void someMethod(String a, int b, double c) public void someMethod(double b, int d, String g) public void myMethod(int c, double m, String p) public void someMethod(int k, double m, String p)
public void someMethod(int k, double m, String p)