+ All Categories
Home > Documents > Java Quiz Bowl

Java Quiz Bowl

Date post: 19-Jan-2016
Category:
Upload: finian
View: 48 times
Download: 1 times
Share this document with a friend
Description:
Java Quiz Bowl. A fun review of the Java you should know from CMPT 201 If you don ’ t know the answers - this week is for you to study up!. Part 1: 10 seconds / question. Each question is followed by the answer. First try to answer the question, then check your answer. - PowerPoint PPT Presentation
68
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Transcript
Page 1: Java Quiz Bowl

Java Quiz BowlA fun review of the Java you should know from CMPT 201

If you don’t know the answers - this week is for you to study up!

Page 2: Java Quiz Bowl

Part 1:

10 seconds / question

Page 3: Java Quiz Bowl

Each question is followed by the answer. First try to answer the question, then check your answer.

Page 4: Java Quiz Bowl

What will this code print:

int x;System.out.println(x);

Page 5: Java Quiz Bowl

Nothing. “x” has been declared but not initialized, which causes a compiler error!

What will this code print:

int x;System.out.println(x);

Page 6: Java Quiz Bowl

What will this code print:

String name;System.out.println(name);

Remember: Strings are NOT primitive types.

Page 7: Java Quiz Bowl

Another compiler error! “name” has been declared but not initialized.

What will this code print:

String name;System.out.println(name);

Page 8: Java Quiz Bowl

What will this code print:

String name;System.out.println(name);

Page 9: Java Quiz Bowl

What is the value of x after this code:

int x = 5 / 2;

Page 10: Java Quiz Bowl

2

What is the value of x after this code:

int x = 5 / 2;

Page 11: Java Quiz Bowl

Will this code compile?

int x = 5;double y = x;

If so, what is the value of y?

If not, fix the code.

Page 12: Java Quiz Bowl

Yes5.0

Will this code compile?

int x = 5;double y = x;

If so, what is the value of y?

If not, fix the code.

Page 13: Java Quiz Bowl

Will this code compile?

double d = 3.14;int i = d;

If so, what is the value of i?

Page 14: Java Quiz Bowl

No.

Will this code compile?

double d = 3.14;int i = d;

If so, what is the value of i?

Page 15: Java Quiz Bowl

What is the value of x that is printed out?

public class Query{

private int x = 5;

public void output() {int x = 10;

System.out.println(x);}

}

Page 16: Java Quiz Bowl

What is the value of x that is printed out?X is equal to 10

public class Query{

int x = 5;

public void output() {int x = 10;

System.out.println(x);}

}

Page 17: Java Quiz Bowl

What are the values of i, j, and k after this code is run:

int i = 5; int j = i++; int k = ++i;

Page 18: Java Quiz Bowl

i = 7j = 5k = 7What are the values of i, j, and k after this code is run:

int i = 5; int j = i++; int k = ++i;

Page 19: Java Quiz Bowl

How many objects are created by this code:

String a = new String(“hello”);String b = a;

Page 20: Java Quiz Bowl

One object.How many objects are created by this code:

String a = new String(“hello”);String b = a;

Page 21: Java Quiz Bowl

Part 2:

30 seconds / question

Page 22: Java Quiz Bowl

Write a for-loop that prints out:

0 2 4 6 8

Page 23: Java Quiz Bowl

Possible Answers

for (int i=0; i<5; i++) System.out.print(2*i + “ “);

for (int i=0; i<9; i+=2) System.out.print(i + “ “);

for (int i=0; i<=8; i=i+2) System.out.print(i + “ “);

Page 24: Java Quiz Bowl

What variables are accessible from inside method “one()”?

public class Question { public int a = 1; private int b = 2; public static int c = 3; private static int d = 4;

public void one (int e){ int f = 5;

} private void two (int g){ int h = 10; } public static void three (int i){ int j = 5;

} private static void four (int m){ int n = 10; }}

Page 25: Java Quiz Bowl

What variables are accessible from inside method “one()”?

public class Question { public int a = 1; private int b = 2; public static int c = 3; private static int d = 4; public void one (int e){ int f = 5;

} private void two (int g){ int h = 10; } public static void three (int i){ int j = 5;

} private static void four (int m){ int n = 10; }}

Page 26: Java Quiz Bowl

What variables are accessible from inside method “two()”?

public class Question { public int a = 1; private int b = 2; public static int c = 3; private static int d = 4; public void one (int e){ int f = 5;

} private void two (int g){ int h = 10; } public static void three (int i){ int j = 5;

} private static void four (int m){ int n = 10; }}

Page 27: Java Quiz Bowl

What variables are accessible from inside method “two()”?

public class Question { public int a = 1; private int b = 2; public static int c = 3; private static int d = 4; public void one (int e){ int f = 5;

} private void two (int g){ int h = 10; } public static void three (int i){ int j = 5;

} private static void four (int m){ int n = 10; }}

Page 28: Java Quiz Bowl

What variables are accessible from inside method “three()”?

public class Question { public int a = 1; private int b = 2; public static int c = 3; private static int d = 4; public void one (int e){ int f = 5;

} private void two (int g){ int h = 10; } public static void three (int i){ int j = 5;

} private static void four (int m){ int n = 10; }}

Page 29: Java Quiz Bowl

What variables are accessible from inside method “three()”?

public class Question { public int a = 1; private int b = 2; public static int c = 3; private static int d = 4; public void one (int e){ int f = 5;

} private void two (int g){ int h = 10; } public static void three (int i){ int j = 5;

} private static void four (int m){ int n = 10; }}

Page 30: Java Quiz Bowl

What variables are accessible from inside method “four()”?

public class Question { public int a = 1; private int b = 2; public static int c = 3; private static int d = 4; public void one (int e){ int f = 5;

} private void two (int g){ int h = 10; } public static void three (int i){ int j = 5;

} private static void four (int m){ int n = 10; }}

Page 31: Java Quiz Bowl

What variables are accessible from inside method “four()”?

public class Question { public int a = 1; private int b = 2; public static int c = 3; private static int d = 4; public void one (int e){ int f = 5;

} private void two (int g){ int h = 10; } public static void three (int i){ int j = 5;

} private static void four (int m){ int n = 10; }}

Page 32: Java Quiz Bowl

Fill in the code to print all the elements of an array to the screen:

public void print (int []array){

}

Page 33: Java Quiz Bowl

public print (int []array){

for (int i=0; i<array.length; i++) System.out.println(array[i]);

}

OR

for (int x : array) System.out.println(x);

Page 34: Java Quiz Bowl

Part 3:

1 minute / question

Page 35: Java Quiz Bowl

Write a Circle class with one instance variable (data field):

radius (double)

No methods necessary.

Page 36: Java Quiz Bowl

public class Circle { private double radius;

}

Page 37: Java Quiz Bowl

Given the current code, what is radius’ value?

public class Circle { private double radius;

}

Page 38: Java Quiz Bowl

radius = 0.0

Instance variables of primitive types are initialized to 0.

Given the current code, what is radius’ value?public class Circle { private double radius;

}

Page 39: Java Quiz Bowl

Given the following code, what is name’s value?

public class Person{ private String name;

}

Page 40: Java Quiz Bowl

name = null

Instance variables of primitive types are initialized to null.

Given the current code, what is name’s value?

public class Person{ private String name;

}

Page 41: Java Quiz Bowl

Add a mutator (setter method) to your Circle class that sets the radius to a specified value.

Page 42: Java Quiz Bowl

public class Circle { private double radius;

public void setRadius(double radius) { this.radius = radius; } }

Page 43: Java Quiz Bowl

Add an accessor (getter method) to your Circle class that gets the radius.

Page 44: Java Quiz Bowl

public class Circle { private double radius;

public void setRadius(double r) { radius = r; }

public double getRadius() { return radius; } }

Page 45: Java Quiz Bowl

Now add a constructor that takes a double as a parameter.

Set the radius to the parameter.

Page 46: Java Quiz Bowl

public class Circle { private double radius;

public Circle(double radius) { this.setRadius(radius);

// or this.radius = radius;

}

public void setRadius(double radius) { this.radius = radius; } public double getRadius() { return radius; } }

Page 47: Java Quiz Bowl

Now add a default constructor to your Circle class.

Set the radius to 1.

Page 48: Java Quiz Bowl

public class Circle { private double radius;

public Circle() { this(1);

// or radius = 1; // or setRadius(1);

} public Circle(double radius) { this.radius = radius; }

public void setRadius(double radius) { this.radius = radius; } public double getRadius() { return radius; } }

Page 49: Java Quiz Bowl

Write a static method that calculates and returns the area of a circle, taking the radius as a parameter.

Page 50: Java Quiz Bowl

public class Circle { private double radius;

public void setRadius(double radius) { this.radius = radius; } public double getRadius() { return radius; }

public static double getArea(double radius){ return Math.PI*radius*radius; } }

Page 51: Java Quiz Bowl

Write a line of code that creates a Circle object. You can assume this is being written in a main method.

Page 52: Java Quiz Bowl

public static void main(String[] args) {

Circle c1 = new Circle();

Circle c2 = new Circle(10);

} }

Page 53: Java Quiz Bowl

Will this code compile and run?

public static void main(String[] args) {

Circle c1;c1 = new Circle();c1 = new Circle(10);

}

Page 54: Java Quiz Bowl

Yes!You can only declare a variable once, but you can set it to new objects multiple times.

Is this code valid?public static void main(String[] args) {

Circle c1;c1 = new Circle();c1 = new Circle(10);

}

Page 55: Java Quiz Bowl

Write a new class ColoredCircle, that is a child of the Circle class.

It should have one additional instance variable (type String) that represents the color of the ColoredCircle

No methods or constructors yet.

Page 56: Java Quiz Bowl

public class ColoredCircle extends Circle { private String color;

}

Page 57: Java Quiz Bowl

Add a default constructor that sets the radius to 1 and the color of the circle to black.

Page 58: Java Quiz Bowl

public class ColoredCircle extends Circle { private String color;

public ColoredCircle() { super(); color = “black”; }}

Page 59: Java Quiz Bowl

Is this code legal?

ColoredCircle c = new ColoredCircle();

c.setRadius(100);

Page 60: Java Quiz Bowl

Yes! ColoredCircle inherits all of the methods in the Circle class!

ColoredCircle c = newColoredCircle();

c.setRadius(100);

Page 61: Java Quiz Bowl

Which of the following code segments are legal?

Circle c1 = new ColoredCircle();

ColoredCircle c2 = new Circle();

Page 62: Java Quiz Bowl

LEGAL:

Circle c1 = new ColoredCircle();

NOT LEGAL:

ColoredCircle c2 = new Circle();

Page 63: Java Quiz Bowl

If the ColoredCircle class had a setColor() method, is this code legal?

Circle c = new ColoredCircle();

c.setColor(“red”);

Page 64: Java Quiz Bowl

No – Circle objects do not have a setColor() method.

Circle c = new ColoredCircle();

c.setColor(“red”);

Page 65: Java Quiz Bowl

Modify the 2nd line of code to make this setColor call legal:

Circle c = new ColoredCircle();

c.setColor(“red”);

Page 66: Java Quiz Bowl

We would need to typecast c.

Circle c = new ColoredCircle();

((ColoredCircle)c).setColor(“red”);

Page 67: Java Quiz Bowl

What if both classes had their own (different) toString() methods? Which would run here:

Circle c = new ColoredCircle();

c.toString();

Page 68: Java Quiz Bowl

The ColoredCircle toString() method would execute:

Circle c = new ColoredCircle();

c.toString();


Recommended