FINAL

¡Supera tus tareas y exámenes ahora con Quizwiz!

De Morgan's Rules

!(A&&B) == !A||!B !(A||B) == !A&&!B

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

17

.substring()

Inclusive of first parameter and exclusive of second parameter. Method that prints part of the String

Return Type

Indicates what data is coming back

Which statement is written properly?

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

What would this program print?

1.0

Client

Instance of the template class

Indexing

Starts at 0

.equals()

Tells if two Strings are exactly the same

Aliases

Two objects refer to the same point in memory or the same object

Logical Operators

!, &&, ||

Math.abs(x)

Absolute value of x

Void

Nothing is coming back

Unboxing

Opposite of autoboxing and conversion of a wrapper class to a primitive

Non-Void Methods

Print value, store data in another variable, and use it in an expression

.charAt()

Prints single letter that is the index

Signature

Provides information on how it will function

Math.random()

Returns a value between 0 and 1

.length()

Returns the number of characters and starts at 1

Math.pow(base, exponent)

Returns the value of the base ^ to the exponent

Scope

Confines in which the variable can be used and where the variable lives

Wrapper Classes

Contains "wraps" of primitive types to objects as an object

Autoboxing

Converting a primitive value into an object of the corresponding wrapper class

Short Circuit Evaluation

For, And, or, Or. If the first one is false, it does not look at the second one

.compareTo()

Lexicographical Spectrum to compare distance between letters

Conditionals

Make decisions

Static Methods

Methods in Java that can be called without an object

Return Statement

Statement that returns data

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.

Nesting

The process of adding a control structure with another control structure. If inside of if

Logical equality compares the data of objects.

True

Reference Equality

Compares objects with reference variables using ==

Logical Equality

Compares two Strings using .equals()

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

9

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

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

50

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.

Relational Operators

==, <, >, <=, >=, !=, =

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.

Behavior

Actions that can be performed

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.

Final

Can only take one value

Immutable

Cannot be changed once it is created

What is casting in Java?

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

String Literal

Code in quotation marks

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

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.

Implicit Conversion

Java automatically turns a primitive into a String and data types operate differently than you might expect

Instance Method

Non-Static Method that needs an object to be called

Math.sqrt(x)

Square root of x

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

.indexOf()

Will return the first occurrence in a String of where the letter is. If it can't find it, it prints -1

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.

Escape Sequences

\" prints quotes \\ prints \ \n new line \+ tab

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.

Dot Annotation

objectName.method();

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;

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

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 of the following is an example of calling a static method?

Math.abs(x)

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.

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!

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

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

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

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

Class

Template for the object

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

The .equals() String method.

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

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

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

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)

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

true

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

!=

Which of the following is a logical operator?

!

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

!A && !B

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

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

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();

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

Casting

Converts a double to an int

Parameters

Formal word for data passing through methods

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

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

toString

What does the expression 80 >= 80 evaluate to?

true

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.

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.

What does the keyword final do?

It prevents variables from being altered.

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

No

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

Pineapple FancyPineapple(String color, int age)

Objects

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

Which of the below is NOT a Java arithmetic operator?

#

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

'C'

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

int myNumber = 10;

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

true

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

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

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 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 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

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

False

Method Overloading

Having multiple constructors with the same name but different formal parameters

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.

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!

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 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 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");

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

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

The value that a method outputs is called:

a return value.

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 of the methods is implemented correctly with respect to the method's return type?

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

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

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.


Conjuntos de estudio relacionados

Marriage & Family Exam 2 (CH 5-8)

View Set

Chapter 29: Perioperative Nursing

View Set

Biology in Focus: Chapter 7 Cellular Respiration

View Set

ACCT 4404 - Income Tax I: Chapter 13

View Set