Midterm Reviewer Flashcards
A component of a program that knows how to perform methods and interact with other objects.
Object
Does not occupy any memory space and is only a logical representation of data.
Class
How is an object created?
An object is created when a class is instantiated.
Java Virtual Machine executes much faster. To which Java characteristic does this pertain?
High-Performance
Complete date when JDK (Java Development Kit) 1.0 was released.
January 23, 1996
A reserved area in memory that holds a value which can be changed while the program is being executed
Variable
Created using defined constructors of classes and used to access objects.
Reference Data Types
A block of code similar to a method and is called when an instance of class is created.
Constructor
A collection of statements grouped together to perform an operation.
Method
A type of constructor wherein the signature is the same with the default constructor, but the body may have code/statements.
No-argument Constructor
What is the output of the following code?
public class MainClass {
public static void main(String[] args) {
char letter = 65;
System.out.println(“Letter is “ + letter);
}
}
Error
What is the output of the following code?
public class MainClass {
public static void main(String[] args) {
int number= 50;
char letter= ‘w’;
System.out.print(number);
System.out.print(letter);
}
}
50w
Variables, methods and constructors which are declared public can be accessed by any class (True or False)
TRUE
Variables defined inside methods, constructors or blocks.
Class Variable
Keywords and identifiers can be used as variable names in Java (True or False)
FALSE
What is the output of the following code?
public class SampleClass { public static void main(String args[]) { int g = 3; System.out.print(++g * 8); } }
32
Which of the following is not a use of the this keyword in Java?
A. Passing itself to the method of the same class.
B. Passing itself to another method.
C. Referring to the instance variable when a local variable has the same name.
D. Calling another constructor in constructor chaining
A
What is the output of the following code?
public class Square { int width; int height; int length; } public class MainClass { public static void main(String args[]) { Square s = new Square(); s.width = 10; s.height = 2; s.length = 10; int box = obj.width * obj.height * obj.length; System.out.print(box); } }
200
What is the output of the following code?
public class MainClass { public static void main(String args[]) { int series[] = {1, 2, 3, 4, 5}; for ( int num = 0; num < series.length - 2; ++num) System.out.println(series[num] + " "); } }
1 2 3
What is the output of the following code?
public class RecurClass { int recur(int num) { int result = 0; if (num == 1) return 1; result = recur(num - 1); return result; } } public class MainClass { public static void main(String args[]) { RecurClass r = new RecurClass() ; System.out.print(r.recur(10)); } }
1
What is the output of the following code?
public class SampleClass {
final int number = 100;
public static void main(String[] args) {
System.out.println(“Number is “ + number);
}
}
ERROR
What is the output of the following code?
public class SampleClass {
public static void main(String[] args){
boolean firstBool = true; boolean secondBool = true; if(b1 == b2){ System.out.print("Domain"); } if(b1.equals(b2)){ System.out.print("Expansion"); } }
}
ERROR
What is the output of the following code?
public class SampleClass{
public static void main(String[] args) { byte firstByte = 150, secondByte = 30; byte thirdByte = firstByte / secondByte; System.out.println("Result is " + thirdByte); } }
ERROR
What is the output of the following code?
public class SampleClass{
public static void main(String[] args){ int try = 2; for(int count = 0; count < try; count++){ System.out.print("Value of count is " + i + ". "); } } }
ERROR
What is the output of the following code?
class PrintClass{
public PrintClass(double num){ System.out.print("First Constructor"); } public PrintClass(float num){ System.out.print("Second Constructor"); } }
public class SampleClass{
public static void main(String[] args){ int longNum = 20; PrintClass pc = new PrintClass(longNum); } }
Second Constructor
What is the output of the following code?
class CharCheckerClass{
public CharCheckerClass(int num){ System.out.print("Number Constructor"); } public CharCheckerClass(char charac){ System.out.print("Char Constructor"); } void CharCheckerClass(String string){ System.out.print("String Constructor"); } }
public class SampleClass{
public static void main(String[] args){ String charac = "c"; CharCheckerClass one = new CharCheckerClass(charac); } }
ERROR
What is the output of the following code?
class CheckerClass{
public CheckerClass(int i){
System.out.println(“This is an integer.”);
}
public void CheckerClass(short s){ System.out.println("This is a short."); } }
public class SampleClass{
public static void main(String[] args){ short shorty = 100; CheckerClass one = new CheckerClass(shorty); } }
This is an integer.
What is the output of the following code?
package ClassPackage;
class FirstClass{
public FirstClass(){
System.out.print(“One”);
}
}
class SecondClass{
public SecondClass(){
System.out.print(“Two”);
}
}
class ThreeClass{
public ThreeClass(){
System.out.print(“Three”);
}
}
public class SampleClass{
static FirstClass first = new FirstClass(); SecondClass second = new SecondClass(); static ThreeClass third = new ThreeClass(); public static void main(String[] args) { System.out.print("Hello"); SampleClass sample = new SampleClass(); } }
OneThreeHelloTwo
What is the output of the following code?
public class SampleClass{
public static void main(String[] args){ int number = 97; switch(number){ case 95 : System.out.print("First "); case 96 : System.out.print("Second "); case 'a' : System.out.print("Third "); case 'b' : System.out.print("Fourth "); case 'c' : System.out.print("Fifth "); } } }
Third Fourth Fifth
What is the output of the following code?
public class SampleClass{
public static void main(String[] args){
int number1 = 0;
while(number1 + 2 < 50){
number1 += 2;
int number2 = 50;
while(number2 - 2 > 0){
number2 -= 2;
}
}
System.out.println(number1 + “ “ + number2);
}
}
Error
What is the output of the following code?
public class SampleClass{
public static void main(String[] args){ int counter = 561; if(counter < 2000){ String stringy = "Lesser than the number."; }else{ String stringy = "Greater than the number."; } System.out.println(stringy); } }
ERROR
What is the output of the following code?
class ObjectClass{
void ObjectClass(){
System.out.println(“Instantiation complete.”);
}
}
public class SampleClass{
public static void main(String[] args){
ObjectClass one = new ObjectClass();
}
}
No output
What is the output of the following code?
public class SampleClass{
public static void main(String[] args){ for(int num1 = 0; num1 < 3; num1++){ for(int num2 = 0; num2 < 3; num2++){ if(num1 == num2) continue; System.out.print(num1 + " " + num2 + ", "); } } } }
0 1, 0 2, 1 0, 1 2, 2 0, 2 1,
What is the output of the following code?
public class SampleClass{
public static void main(String[] args){ for(int number = 0 ; number < 5 ; number++) System.out.println(number + " "); System.out.println(number + " "); }
}
ERROR
What is the output of the following code?
public class SampleClass{
static String stringy = "false"; public static void main(String[] args) { if(stringy == false) System.out.println("Nothing to see here."); } }
ERROR
What is the output of the following code?
class Hero {
public void smash() { System.out.println(“Detroit Smash!”); }
}
class Student extends Hero {
private void smash() { System.out.println(“Practice Smash!”); }
}
public class SampleClass {
public static void main(String args[]) {
Hero deku = new Student();
deku.smash();
}
}
Compile-time Error
What is the output of the following code?
class Hero {
private void smash() { System.out.println(“Detroit Smash!”); }
}
class Student extends Hero {
public void smash() { System.out.println(“Practice Smash!”); }
}
public class SampleClass {
public static void main(String args[]) {
Hero deku = new Student();
deku.smash();
}
}
Compile-time Error
What is the output of the following code?
class Kage {
public void fight() {
System.out.println(“Flying Raijin!”);
}
}
class Jonin extends Kage {
public void fight() {
System.out.println(“Rasengan!”);
}
}
class Shinobi extends Jonin {
public void fight() {
super.super.fight();
System.out.println(“Shadow Clone Technique!”);
}
}
public class SampleClass {
public static void main(String[] args) {
Shinobi ninja = new Shinobi();
ninja.fight();
}
}
Compile-time Error
What is the output of the following code?
class Kage {
public void fight() {
System.out.println(“Flying Raijin!”);
}
}
class Jonin extends Kage {
public void fight() {
System.out.println(“Rasengan!”);
}
}
class Shinobi extends Jonin {
public void fight() {
super.fight();
System.out.println(“Shadow Clone Technique!”);
}
}
public class SampleClass {
public static void main(String[] args) {
Shinobi ninja = new Shinobi();
ninja.fight();
}
}
Rasengan!
Shadow Clone Technique!
What is the output of the following code?
final class DifficultClass {
private final double num1; private final double num2; public DifficultClass(double num2, double num1) { this.num1 = num2; this.num2 = num1; } public String printLine() { return "" + num2 + " + " + num1 + "."; } }
public class SampleClass {
public static void main(String args[])
{
DifficultClass c = new DifficultClass(150, 270);
System.out.println(“Numbers are “ + c.printLine());
}
}
Numbers are 270.0 + 150.0.
What is the output of the following code?
class MoreDifficultClass {
public MoreDifficultClass() { System.out.println("Let's make it challenging."); } public MoreDifficultClass(int num1) { System.out.println("Print Here!"); } }
class DifficultClass extends MoreDifficultClass {
private final double num1; private final double num2; public DifficultClass(double num2, double num1) { this.num1 = num2; this.num2 = num1; } public String printLine() { return "" + num2 + " + " + num1 + "."; } }
public class SampleClass {
public static void main(String args[])
{
DifficultClass c = new DifficultClass(150, 270);
System.out.println(“Numbers are “ + c.printLine());
}
}
Let’s make it challenging.
Numbers are 270.0 + 150.0.
What is the output of the following code?
class MoreDifficultClass {
public MoreDifficultClass() { System.out.println("Let's make it challenging."); } public MoreDifficultClass(int num1) { System.out.println("Print Here!"); } }
class DifficultClass extends MoreDifficultClass {
private final double num1; private final double num2; public DifficultClass(double num2, double num1) { this.num1 = num2; this.num2 = num1; } public String printLine() { return "" + num2 + " + " + num1 + "."; } }
public class SampleClass {
public static void main(String args[])
{
DifficultClass c = new MoreDifficultClass(150);
System.out.println(“Numbers are “ + c.printLine());
}
}
Compile-time Error
What is the output of the following code?
class MoreDifficultClass {
public MoreDifficultClass() { System.out.println("Let's make it challenging."); } public MoreDifficultClass(int num1) { System.out.println("Print Here!"); } public String printLine() { return "Successful printing!"; } }
class DifficultClass extends MoreDifficultClass {
private final double num1; private final double num2; public DifficultClass() { num1 = 340; num2 = 580; System.out.println("Printing Hatdog!"); } public DifficultClass(double num2, double num1) { this.num1 = num2; this.num2 = num1; } public String printLine() { return "" + num2 + " + " + num1 + "."; } }
public class SampleClass {
public static void main(String args[])
{
MoreDifficultClass c = new DifficultClass();
System.out.println(“Numbers are “ + c.printLine());
}
}
Let’s make it challenging.
Printing Hatdog!
Numbers are 580.0 + 340.0.
What is the output of the following code?
class Jujutsu
{
int showJujutsu(int num)
{
num /= 50;
return num; } }
class SpecialGrade extends Jujutsu
{
int showSorcerer(int num)
{
num *= 10;
return showJujutsu(num); } }
public class SampleClass
{
public static void main(String[] args)
{
SpecialGrade gojo = new SpecialGrade();
System.out.println(gojo.showSorcerer(500));
}
}
100
What is the output of the following code?
class Resource {
String resourceName = ""; float resourceSalary = 0; public void printSalary() { resourceSalary += 10000; System.out.println("Salary is " + resourceSalary); } }
class Analyst extends Resource {
public void printSalary() { resourceSalary += 30000; System.out.println("Salary is " + resourceSalary); }
}
public class SampleClass {
public static void main(String[] args) { Resource s = new Resource(); Analyst a = new Analyst(); Resource res = new Analyst(); s.printSalary(); a.printSalary(); res.printSalary(); } }
Salary is 10000.0
Salary is 30000.0
Salary is 30000.0
What is the output of the following code?
class Resource {
String resourceName = ""; float resourceSalary = 0; public void printSalary() { resourceSalary += 10000; System.out.println("Salary is " + resourceSalary); } }
class Analyst extends Resource {
public void printSalary() { resourceSalary += 30000; System.out.println("Salary is " + resourceSalary); }
}
public class SampleClass {
public static void main(String[] args) { Resource s = new Resource(); Analyst a = new Analyst(); Analyst ana = new Resource(); s.printSalary(); a.printSalary(); ana.printSalary(); } }
Compile-time Error
What is the output of the following code?
class Resource {
String resourceName = ""; float resourceSalary = 0; public void printSalary() { System.out.println("Salary is " + resourceSalary); } public void printSalary(float resourceName) { this.resourceSalary = resourceName; System.out.println("Salary is " + resourceSalary); } public void printSalary(String resourceName, float resourceSalary) { this.resourceName = resourceName; this.resourceSalary = resourceSalary; System.out.println("Salary is " + resourceSalary); } }
class Analyst extends Resource {
public void printSalary() { resourceSalary += 30000; System.out.println("Salary is " + resourceSalary); }
}
public class SampleClass {
public static void main(String[] args) { Analyst a = new Analyst(); a.printSalary("All Might", 10000); a.printSalary(); a.printSalary(19000); } }
Salary is 10000.0
Salary is 40000.0
Salary is 19000.0
What is the output of the following code?
class Resource {
String resourceName = ""; float resourceSalary = 0; public void printSalary() { System.out.println("Salary is " + resourceSalary); } public void printSalary(float resourceName) { this.resourceSalary = resourceName; System.out.println("Salary is " + resourceSalary); } public void printSalary(String resourceName, float resourceSalary) { this.resourceName = resourceName; this.resourceSalary = resourceSalary; System.out.println("Salary is " + resourceSalary); } }
class Analyst extends Resource {
public void printSalary() { resourceSalary += 30000; System.out.println("Salary is " + resourceSalary); }
}
public class SampleClass {
public static void main(String[] args) { Analyst a = new Resource(); a.printSalary("All Might", 10000); a.printSalary(); a.printSalary(19000); } }
Compile-time Error
What is the output of the following code?
class Resource {
String resourceName = ""; float resourceSalary = 0; public void printSalary() { System.out.println("Salary is " + resourceSalary); } public void printSalary(float resourceName) { this.resourceSalary = resourceName; System.out.println("Salary is " + resourceSalary); } public void printSalary(String resourceName, float resourceSalary) { this.resourceName = resourceName; this.resourceSalary = resourceSalary; System.out.println("Salary is " + resourceSalary); } }
class Analyst extends Resource {
public void printSalary() { resourceSalary += 30000; System.out.println("Salary is " + resourceSalary); }
}
public class SampleClass {
public static void main(String[] args) { Resource a = new Analyst(); a.printSalary("All Might", 10000); a.printSalary(); a.printSalary(19000); } }
Salary is 10000.0
Salary is 40000.0
Salary is 19000.0
What is the output of the following code?
import java.util.ArrayList;
public class SampleClass {
public static void main(String[] args) { Integer i = new Integer(3000); int num1 = i.intValue(); int num2 = i; ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(num1); numbers.add(num2); for(Integer integer: numbers) { System.out.print(integer); } } }
30003000