Because Java uses both compilation and interpretation it is considered a _____ language
Hybrid
To continue an existing branching statement, I can use this to add an additional logical expression.
else if
This type of loop is often considered a "post-check" loop.
do-while
When dealing with multiple classes, the java main method must exist in the ____ class
Main, Primary, Top-most.
output the constant PI, formatted to 3 decimal places.
System.out.printf("%.3f", Math.PI);
This type of error occurs most frequently when the syntax is correct, but you create an illegal operation. Such as attempting to enter a string for Scanner's nextInt() method.
Runtime error
break
Infinite loop
while(true)
List the 4 fundamentals of Object Oriented Programming
Encapsulation, Inheritance, Abstraction, Polymorphism
Create a method that returns the sum of 2 given parameters.
public int Add(int a, int b){
return a+b;
}
To call any method in java, you are required to use
Parenthesis
A decision structure "flag" is often a single variable of what datatype?
Boolean
This keyword skips sections of loop code to move on to a new iteration of the same loop.
continue
These methods have the same name as another existing method, but different parameters.
Overloaded methods.
Debug the following loop-
do - needs curly brackets
missing semicolons on some lines.
System.out.println (not .in, or printline)
the "not" keyword does not exist in java. count != 5
What class/package do I need to import in order to create Times and Dates in code?
java.time.LocalDate
java.time.LocalDateTime
what is the output of the following switch pseudocode?
letterGrade = C
switch(letterGrade){
case A
output "excellent work"
case B
output "decent job"
break
case C
output "passable"
case D
output "needs improvement"
break
default
output failed
}
passable
needs improvement
To validate user input with a while loop, the Scanner class has these type of methods to assist.
hasNext-X()
What kind of methods belong to a class itself, rather than an instance of the class?
Static methods.
Write a java code snippet that validates user input to be a positive integer.
Scanner scan = new Scanner(System.in);
int num;
while (!scan.hasNextInt() || (num = scan.nextInt()) <= 0){
//output invalid
if(!scan.hasNextInt()){
scan.next();
}
}
Explain the purpose of the Java Virtual Machine (JVM)
It is the environment that surrounds all java programs. A java program cannot be executed without the JVM.
This method of branching uses the '?' operator and as times is a replacement for an if/else.
Ternary
What is the output of the following loop?
String text = "burger king";
for(int x = 0; x < text.length(); x++){
if(x % 2 == 0){
System.out.print(text.charAt(x));
}
}
bre ig
A(n) ____ is an instance of a class that bundles together data. A good example would be a String, whereas primitive datatypes are not an example.
An object.
Create an encapsulated class of your choice. With at least 1 method.
Create the object for the class and call its method in your main java program.
class calculator{
private int a;
private int b;
public calculator(int a, int b){
this.a = a;
this.b = b;
}
public int add(){
return a+b;
}
}
//main method call
calculator c = new calculator(2,3);
System.out.println(c.add());