1/53
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Can Java have two variables named name and Name?
Yes, always
What output is produced by:
String greetings = "Hello" + " there " + "earthling";
System.out.println(greetings);
Hello there earthling
Consider the following code:
System.out.println("Please tell us your name");
Scanner keyboardInput = new Scanner(System.in);
What can you do to make sure the input is read?
The code is incomplete
Needs to have something like
String fullName = userInput.nextLine();
There are only two basic types of methods..
Methods that returns a value and methods that do not.
What is the syntax for importing a scanner?
import java.util.*;
How do you declare a variable?
variableType variableName
i.e. double decimals
What does the following boolean expression evaluate to?
((a < 100) && (!(a == 66) || (a == 77)))
True whenever a is less than 100 and is not 66 (or equal to 77); false otherwise
How do you change a variable's type?
By casting,
int b = 0;
(double)b
How to import math?
What are methods you can use within math?
Don't need to import math
Math.abs();
Math.hypot(double x, double y)
Math.max/min(2 numbers);
Math.sqrt();
If boolean variable P is true and Q is false, what is !(P || Q)?
false
What is the value of e?
int a = 4;
int b = 3;
double c = 3.2;
double d = 1.0;
double e;
if ((a / b) < (b / a)) {
e = c / d;
} else {
e = a / b;
}
1.0
First, performs a/b as ints then converts the answer to double
Syntax for switch statements
switch (value being compared to)
{
case 1:
code
break;
case 2;
code
break;
}
when value is equal to case #, the statement executes
int n = 10;
do
{
System.out.println(n);
n = n - 3;
} while (n > 0);
10
7
4
1
What is the output of the following?
for (int count = 1; count < 5; count++)
System.out.print((2 * count) + " ");
2 4 6 8
public static void whileMystery(int x, int y, int z) {
while (x < z || y < z) {
x = x + y;
y = y * 2;
System.out.print(x + " " + y + " ");
}
System.out.println();
}
5 10 15 20
A while loop
will execute if its test condition is true.
What is the expression num *= 3 equivalent to?
num = num * 3;
The boolean, int, and double variable types are examples of
primitive types
A do-while loop
will always execute at least once.
a = false;
b = true;
c = 5 < 3;
x = !( a & b & c) == ( !a | !b | !c);
What is x equal to?
true
Data types of arrays can be
Of any data type, including an array data type
What is the correct way to create an empty integer array that contains 3 elements?
int[] arr = new int[3];
What is wrong with the following code?
int[] nums = 0;
0 is not a valid value for an integer array
What is the outcome of the following code?
int[] a = { 1 , 2, 3, 4, 5 };
int[] b = { 7, 8, 9, 10 };
b = a;
b[4] = 99;
System.out.println(a[4]);
99
A constructor
is executed when an instance is created
Instance variables
are class-level variables
Actions in a class object
are typically non-static methods
Assume a class "Cat" with a toString() method. Which of these would be an acceptable return value?
All of these are acceptable return values for a toString() method
The cat's name
A string that includes all of the Cat instance's properties
"Bob"
All of these are acceptable return values for a toString() method
Assume a class "Dog" with an equals() method.
Which of these would be an acceptable comparison between "this" Dog instance and "another" Dog instance?
Comparison of only Dog's memory locations
Comparison of all Dog's properties
All of these are acceptable comparisons between Dog instances
Comparison of only Dog's name
All of these are acceptable comparisons between Dog instances
Given a class "Ball" with a "radius" property and "getArea()" method, which of these would be an appropriate return statement for a toString() method?
Any of these return statements would be appropriate
return ("Area = " + getArea());
return ("This is a circle.");
return ("Radius = " + this.radius);
Any of these return statements would be appropriate
Which is likely a proper implementation of the "equals" method?
Comparing the values of instance variables that are of primitive data types
Given the following Class
public class LightSaber
{
private static String color;
public LightSaber(String c)
{
this.color = c;
}
}
Let's assume that we create the following in a Driver class:
LightSaber sith = new LightSaber("red");
LightSaber luke = new LightSaber("blue");
LightSaber yoda = new LightSaber("green");
What is the color of the "sith" light saber after the "yoda" light saber is created?
green
What is the main difference between the "while" loop and the "do/while" loop?
The do/while loop is guaranteed to "run" at least once
What is the outcome of the following code?
int sum = 0;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
sum++;
}
}
sum is 100
int sum = 0;
for(int i = 0; i < 10; i++)
{
for(int j = 0; i < 10; i++)
{
sum++;
}
}
What is sum?
10
What will happen when the following code executes?
int sum = 0;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; i++)
{
sum++;
}
}
endless loop
The this keyword
refers to the calling instance object.
How many elements is in an array thisArray?
thisArray.length;
A class that has a main method
can be a main program.
Consider the following code:
int[] a = {4, 7, 2, 1, -2};
int[] b = {a[0], a[1], a[2], a[3], a[4]};
a[2] = a[1] * 3;
b[1] = a[2] - 9;
What is the contents of a after this code is executed?
4, 7, 21, 1, -2
How many iterations will the following loop go through? (I.e., how many times is the loop body executed?)
for (int i = 2; (i > 1) && (i < 10); i = i+2)
{
System.out.println(i);
i = i - 1;
i = 10 / i;
}
1
Object instance variables
store data associated with the object.
A properly written accessor method of a class
returns the value of private instance variables.
Consider the following code:
int[] a = {4, 7, 2, 1, -2};
int[] b = a;
a[2] = a[1] * 3;
b[1] = a[2] - 9;
What is the contents of a after this code is executed?
4, -7, 21, 1, -2
The instance of a class
follows the structure set by the class definition.
Which of the following code snippets creates an instance of the class MyClass?
MyClass aMyClass = new MyClass();
Object methods
act on data connected to or passed into the object.
What is the difference(s) between a public and private instance variable?
Private instance variables cannot be directly read or set while public instances variables can be directly read and set.
Two classes that each have a method of the same name
will work fine.
A properly written mutator method of a class
sets the value of private instance variables.