FINAL comp sci

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Which of the following is a logical operator?

!

Which of the following is the "not equal" symbol?

!=

Which of the following logical statements is equivalent to: !(A || B)

!A && !B

Which of the following logical statements is equivalent to: !(A && B)

!A || !B

The purpose of a wrapper class is to:

"Wrap" a primitive value to convert it to an object

Which of the below is NOT a Java arithmetic operator?

#

Which of the following could be stored in the variable: char initial;

'C'

Operations

+, -, *, /, %, =

What range of numbers would be generated by using the following code: int num = (int)(Math.random() *501)

0-500

What will be stored in the variable modulo after running the following code snippet? public static void main(String[]args) { int num = 2; int modulo = 15; modulo %/ num; }

1

What would this program print?

1.0

public static void main(String[]args) { int age = 16; age++; age--; age++; }

17

What is the output of the following code? String forest = "Amazon Rainforest"; System.out.println("forest.indexOf('a')); System.out.println("forest.indexOf('g')); System.out.println("forest.indexOf('n'));

2 -1 5

Consider the circle class below: public class Circle { private double radius; public Circle(double circleRadius) { radius = circleRadius; } public void setRadius(int newRadius) { radius = newRadius; } pubic void printDiameter() { double diameter = 2 * radius; System.out.println(diameter); } } What is the output of the main method below? public class MyProgram { public static void main(String[]args) { Circle pizza = new Circle(12); pizza.printDiameter(); pizza.setRadius(10); pizza.printDiameter(); } }

24.0 20.0

What will be the output of the following code snippet? public class Calculator { public static void main(String[]args) { int first = 7; int second = 2; int result = first/second; System.out.println(result); } }

3

What is the result of the expression: 150 % 100?

50

What will the expression: (int) 9.9 evaluate to?

9

Which of the following is NOT a relational operator?

=

What is the difference between == and =?

= is used for assignment, while == is used to check for equality.

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 converted to a primitive value.

Which of the following best describes the relationship between a class and an object?

A class definition specifies the attributes and behavior of every object that will be made.

What is a constructor in Java?

A constructor allows us to create a new instance of a class, usually initializing instance variables.

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 an 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 positive number because "Vu" comes after "Lopez" in lexicographical order

Which if these is NOT TRUE about primitives and objects?

A primitive has data and methods associated with it while an object only stores data.

Behavior

Actions that can be performed

Which of the following is true about primitive types in Java?

All of these statements are correct for primitive variables in Java.

Which of the following statements will not compile? You may assume the text in pink is an initialized variable. I. "Tilly is " + age + " years old" II. "My favorite letter is " + 'k' III. greeting + name IV. All of these statements will compile

All of these statements will compile

What is an object in Java?

An object is something that contains both state and behavior.

What value of x would make this boolean expression evaluate to false? (x < 10) && (x != 5)

Any value greater than or equal to 10

What will be the output of the following code snippet? public class Casting { public static void main(String[]args) { int total = 100; int numPeople = 40; double average; average = total/(double) numPeople; System.out.println("Average: " + average); } }

Average: 2.5

What does it mean to be a client of a class?

Being a client of a class means that we can use its methods and functionality without necessarily understanding how it works.

What will be the output of the following code snippet: public class Variables { public static void main(String[]args) { intTotalBirds = 150; double averageTime = 45.7; String mostCommon = "Mallard Duck"; System.out.println("Bird Watching Results"); System.out.print("Total birds seen: "); System.out.println(totalBirds); System.out.print("Average time between sightings: "); System.out.println(averageTime); System.out.print("Most common bird seen was "); System.out.println(mostCommon); } }

Bird Watching Results Total birds seen: 150 Average time between sightings: 45.7 Most common bird seen was Mallard Duck

The following code snippet should print the letter grade associated with the integer value in grade. However, the if-else statements are in the wrong order. What order should these statements be in such that the letter grade is A if the grade is above 90, B if it is between 80 and 90, C if it is between 70 and 80, and D if the grade is less than 70? A) else if(grade < 80) { System.out.println("C"); } B) else { System.out.println("A"); } C) if(grade > 70) { System.out.println("D"); } D) else if(grade > 80) { System.out.println("B"); }

C,A,D,B

Final

Can only take one value

What is casting in Java?

Casting is turning a value of one type into another type.

String Literal

Code in quotation marks

Casting

Converts a double to an int

State

Data of the object

Every class definition has each of the following EXCEPT:

Defined objects as copies of the class

Null

Does not point to any data and has no memory

Consider this class definition of a Pineapple. public class Pineapple { private boolean isRipe; private String color; private double weight; //Rest of class goes here } When we use this class to create Pineapple objects, which of the following is guaranteed to be true?

Every Pineapple object will have the same attributes.

Consider this Circle class: public class Circle { private int radius; public Circle(int theRadius) { radius = theRadius; } public void setRadius(int newRadius) { radius = newRadius; } public int getRadius() { return radius; } public boolean equals(Circle other) { return radius == other.getRadius(); } } Using the Circle class, what is the output of the following code snippet? Circle one = new Circle(10); Circle two = new Circle(10); System.out.println(one == two);

False

What is the value of cannotNightBike if hasHeadlight is true and hasBikeLight is false? - boolean cannotNightBike = !(hasHeadlight || hasBikelight);

False

Parameters

Formal word for data passing through methods

Method Overloading

Having multiple constructors with the same name but different formal parameters

What is the output of this program? int numTreeRings = 50; System.out.println("How old is the tree?"); if(numTreeRings < 20) { System.out.println("Still young!"); } if(numTreeRings < 50) { System.out.println("Pretty old!"); } if(numTreeRings < 100) { System.out.println("Very old!"); }

How old is the tree! Very old!

What is the output of this program? int numTreeRings = 50; System.out.println("How old is the tree?"); if(numTreeRings > 10) { System.out.println("Still young!"); } if(numTreeRings > 40) { System.out.println("Pretty old!"); } if(numTreeRings > 150) { System.out.println("Very old!"); }

How old is the tree? Still young! Pretty old!

Suppose a program is a client of the Player class. Here is a snippet of code contained in the program: (Notice the second line is the signature for the constructor) Player firstPlayer = new Player("Karel", "Warrior", "Mote Prime", 90); Player Player(String name, String role, String location, int health); Using the code above, where would find the formal parameters?

In the documentation.

What is the purpose of overloading a class' constructor?

It allows the user to set the values of different combinations of the instance variables when the object is created.

What is the output of this program? int phLevel = 9; if(phLevel < 7) { System.out.println("It is acidic!"); } if(phLevel > 7) { System.out.println("It is basic!"); } if(phLevel == 7); System.out.println("It is neutral!); }

It is basic!

What does the keyword final do?

It prevents variables from being altered.

What would be printed by this code snippet? String language = "Java"; String opinion = " is fun!"; System.out.println(language + opinion);

Java is fun!

Which of the following is an example of calling a static method?

Math.abs(x)

In the code below, will the 5/0 ==0 be evaluated? boolean shortCircuit = true || (5/0 == 0);

No

Strings are immutable. This means that:

Once a String variable has been assigned a value, the value cannot be modified but the variable can be assigned to a different value.

Which of the following is NOT a valid way to overload this constructor: Pineapple Pineapple(String Color);

Pineapple FancyPineapple(String color, int age)

char combo = 'B'; if(combo == 'A') { System.out.println("Tamale it is!"); } else if(combo == 'B') { System.out.println("Quesadilla it is!"); } else { System.out.println("That is not a combo of ours"); }

Quesadilla it is!

Which of the following describes the difference between static methods and instance methods?

Static methods can be called without using an object while instance methods need to be called on an object.

Reference Variable

Store the address of the variable

Which of the choices below is not a primitive type in Java?

String

Which of the following would properly print this quote by Edsger W Dijkstra - an early pioneer of computer science - as show below? "Testing shows the presence, not the absence of bugs." --- Edsger W Dijkstra

System.out.println("\"Testing shows the presence, not the absence of bugs \""); System.out.println("--- Edsger W. Dijkstra");

Class

Template for the object

What is the proper way to compare String values in Java?

The .equals() String method.

What is the output of this code? String firstName = "Karel"; String lastName = "The Dog"; if(firstName.length() > lastName.length()) { System.out.println(firstName + " is longer than " + lastName); } else { System.out.println(lastName + " is longer than " + firstName); }

The Dog is longer than Karel

What are parameters?

The formal names given to the data that gets passed into a method.

A reference variable holds a special value. What is this special value?

The memory address of an object

What will be the output of the following code snippet and why? public static void main(String[]args) { int num = 2; int dividend = 5; dividend /= num; System.out.println(dividend); }

The output will be 2 because when two integers are divided in Java, the decimal portion is always truncated.

public static void main(String[] args) { final int z; z = 20; z= 30; System.out.println(z); } What value of z is actually printed?

This code gives and error.

Why do we use if statements in Java?

To do something only if a condition is true

Logical equality compares the data of objects.

True

Reference equality uses the equality operator (==) and compares the references (addresses in memory) of two objects.

True

What is the result of the value for cannotBike in the following code if boolean variable hasBike is true and hasHelmet is false? - boolean cannotBike = !(hasBike && hasHelmet);

True

public class Circle { private int radius; public Circle(int theRadius) { radius = theRadius; } public void setRadius(int newRadius) { radius = newRadius; } public int getRadius() { return radius; } public boolean equals(Circle other) { return radius == other.getRadius(); } } Using the same circle class, what is the output of this code snippet: public void run () { Circle one = new Circle(5); Circle two = new Circle(10); foo(two); System.out.println(one.equals(two)); } public void foo(Circle x) { x.setRadius(5); }

True

What is the output of the following code snippet? double ticketPrice = 12.75; if(ticketPrice > 15) { System.out.println("Too expensive!"); } else if(ticketPrice > 10) { System.out.println("Typical"); } else if(ticketPrice > 5) { System.out.println("Great deal!"); } else { System.out.println("Must be a scam"); }

Typical

Objects

Variable of a data type that is user defined and has a state and a behavior

Which of the following is NOT part of the constructor signature? (Signature is what it's called when you instantiate it)

Which instance variables are initialized

In the code below, will the 5 / 0 == 0 be evaluated? boolean shortCircuit = true && (5/0 == 0);

Yes

You are using a class as a client. What would you need to know in order to create an object of the class you intend to use?

You need to know the formal parameters in order to pass in actual parameters.

The value that a method outputs is called:

a return value.

Suppose you have a class called Elevator. The Elevator class has a method called goingUp, which is partially defined below: public boolean goingUp() { //code omitted } Which of the following statements correctly stores the return value of goingUp when it is called on the Elevator object named hotel?

boolean up = hotel.goingUp();

What is the difference between the int type and the double type?

double can store numbers like 1, 3, 3.5, -4, but int can only store numbers like 1, 4, -7, 10.

Consider the code snippet below. String name = "Karel"; String checkName = new String("Karel"); boolean nameMatches = name == checkName; Note that I had to force Java to create a new String object by using new and calling the String constructor. Recall that if you set two String variables to the same String literal, Java tries to be efficient and uses the same object. With this in mind, what will the value of nameMatches be?

false

Which statement is written properly?

if(boolean expression) { //code to execute }

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?

int area = shape.getArea();

Which of the following is a proper way to declare and initialize a variable in Java?

int myNumber = 10;

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) { isHonor = status; } Using the Student object called mikey, which of the following is the correct way to set mikey's honor status to true?

mikey.setHonorStatus(true);

What is the importance of the null value?

null allows a reference variable to be empty and not hold any memory address.

Which of the following variable names follows best practices for naming a variable?

numApples

Which of the methods is implemented correctly with respect to the method's return type?

public String getColor() { return "Red"; }

Consider this code snippet: int roomHeight = 40; int roomWidth = roomHeight * 3; Rectangle room = new Rectangle(roomHeight, roomWidth); Which of the following is a reference variable?

room

Consider the following methods involving strings and integers: public void update(String x, int y) { x = x + "Retriever"; y = y * 3; } public void myTest() { String s = "Golden"; int num = 7; update(s, num); /*end of method*/ } When a call to myTest() happens, what are the values of s and num at the point indicated by the /*end of method*/

s is the string "Golden"; num = 7

Which of the following choices is a formal parameter of the constructor? /**The Shark class describes a shark * *Every shark has a region where it lives and an age * */ public class Shark { //Attributes private String habitat; private int age; public Shark(String region, int sharkAge) { habitat = region; age = sharkAge; } }

sharkAge

Which of the following boolean expressions will be true after the following code snippet executes? String str1 = new String("Cutch"); String str2 = "RSChs"; String str3 = "Cutch"; str2 = str1;

str1 == str2 && str1.equals(str3)

Refer to the Card class shown below. public class Card { private String suit; private int value; //13 values for each suit in deck (0 to 12) public Card(String cardSuit, int cardValue) { /* implementation */ //Rest of class goes here } Which of the following is the correct /* implementation */ code for the constructor in the Card class?

suit = cardSuit; value = cardValue;

What method must a class implement in order to concatenate an object of the class with a String object?

toString

An if- else if statement must always start with an if statement.

true

What does the expression 80 >= 80 evaluate to?

true

What does the following expression evaluate to: true && !false

true

int x = 7; double y = 2.0; boolean z = false; x = x + 3; z = true; What are the memory values associated with variables x, y, and z after the code snippet executes?

x holds the int value 10, y holds the double value 2.0 and z holds the boolean value true.


Set pelajaran terkait

Chapter 11: Human Resource Management: Finding and Keeping the Best Employees

View Set

EMT Module Exam 10 (Chapters 35 - 40)

View Set

Money & Banking 5.5, Chapter 5, 14,15,16 (Mid-Term), Money and Banking CH 5, Econ Chapter 5 part 2, econ206 chapter 5, Econ 2035 Ch5.1, 2035 Chapter 5, Money and Banking Ch 5, Chapter 3 FINC 3700, Chapter 4, Econ 2035 Ch4.3, Money & Banking HW #2, Ec...

View Set

A/V technology Careers and Education

View Set

ENT4113 FIU FINAL | Entrepreneurship SUCCESSFULLY LAUNCHING NEW VENTURES

View Set

Mandated exceptions & Permitted exceptions to confidentiality

View Set