What gets printed from the following code?
System.out.println("**");
System.out.print("*");System.out.print("*");
**
**
Write the code statement for a new variable named alex with a value of 12.
int alex = 12;
double alex = 12.0;
What is casting?
Turn one primitive type into a different primitive type (usually with numbers)
What Java keyword is used when instantiating an object?
new
What is the return type of the following method?
public double myMethod(int a, boolean b)
double
What gets printed from the following code?
String str = "basic vs c++";
System.out.println(str.substring(2));
sic vs c++
(9 + 10) % 12
7
Write the code statement to turn the following code into an int.
double dub = 22.0;
(int) dub
int x = (int) dub;
Write the code for a constructor for the class named Person.
public Person() {}
public Person(String name) { ... }
True/False: Objects and primitives both store and pass values when used
False, Objects store addresses
What is the value of str after the code executes?
String str = "Hello World"
str.substring(0, 5);
"Hello World"
Name 4 different primitive types
int, double, boolean, char, byte, long, short, float
What is the value of d after the code executes?
double d = 5.0;d += (double) (7 / 2);
8.0
A Class is a template for an object. Explain what each of the following are relative to classes: instance variables, attributes, constructor, methods.
Instance variables: data stored
Attributes: other term for instance variables
Constructor: used to create instances of the class
Methods: behavior of the object
public WindTurbine(double e) {
efficiencyRating = e; }
Write the code statement to instantiate a WindTurbine object wt that has an efficiency of 0.75.
WindTurbine wt = new WindTurbine(0.75);
What is the value of str after the code executes?
String str = "my store";
str += str.substring(0, 3);
"my storemy"
The following code is used to perform some sort of arithmetic function. Give this function an appropriate name for what it does.
int _____ (int x) { return x % 100 / 10; }
getTensDigit() or something similar
System.out.println((char)('A' + 7));
'H'
What is method overloading and when can it NOT happen?
Same method name, different parameters. When two methods with the same name and parameter types exist in the same class.
When calling a method with formal parameters, the actual parameters passed must have _____ types.
matching, the same, etc
What gets printed from the following code?
String str = "basic vs c++";
System.out.println(str.indexOf("basic") + 0 + str.substring(5, 9) + str.indexOf("basic") + 1);
0 vs 01
What gets printed from the following code?
System.out.println(5 + 4 / (16 % 4));
ArithmeticException, divide by 0
What is the value of i after the code executes?
final int i = (int) 10.0;i = (int) ((double) 5 / 2);
An Object's toString() method can be overridden to print more user friendly and useful text. What is the default functionality of the toString() method?
What gets printed from the following code?
System.out.println(0 + "0" + 1 + 2);
0012