Java Basics (Ch.1)
Loops
General
Branching
Formatting
Inheritance
Arrays
Class Creation
Misc.
100

What is the file extension that my code is saved as after compiling.

.class

100

Which of the following is not a loop type: 

If-Else

For

Do-While

While

If-Else

100

You are supposed to write this out before starting any code. It describes what your code does.

Algorithm

100

What does a branch statement take advantage of in order to control flow? 



Hint: It rhymes with shmoolean shmexpression.


boolean expression

100

10/2.0 would return what?

5.0


Why is it not 5?

100

How many children can each parent class have?

How many parents can a child class have?

Infinite

1

100

How do you create an array of integers with size of 10? 

Can my array grow after being built at size 10?

int[] numbers = new int[10];

No, I would have to create a new array with the larger size.

100

How do I start out a Class? What is the first line?


Assume our Class name is MyClass for this question. Also assume our class is a normal concrete class.

public class MyClass 

{

}

100

Does every class required to have its own main method?

No, you can have a class that is only ever used via a second class.

200

The Java compiler translates your Java program into what? 

Byte-Code.

200

Is this a legal use of an if-else statement? Assume there is no other code above.

int i = 0;

else 

{

  i--;

}

if(i == 0) 

{

  i++;

}

No. You cannot have an else without an if. 

How would you fix this in this particular case?

200

What is the % used for in java?


What is it called?

To return the remainder. 

Ex: 14%3 = 2 

It is called a modulo or mod.

200

A Switch statements case can be type 

char, int, short, byte or ???

String

200

double price = 19.8;

If you already have imported the DecimalFormat class and have an object:

DecimalFormat twoDP = new DecimalFormat("0.00");


How would I output my price to the screen? Including a dollar sign.

System.out.println("$" + twoDP.format(price));

or 

System.out.print("$" + twoDP.format(price));

200

What is the syntax required to inherit from another class?


Use "Parent" as the name of parent class and 

"Child" as the name of the child class.

public class Child extends Parent

200

What are the two ways we have used for traversal of an array?

For loop and for-each loop.

200

What are the minimum things every concrete class should have?

1) Instance Variables

2) Constructors (Default, Full, and maybe Copy) 

3) Getters and Setters for every instance variable unless you have a good reason not to.

4) Equals() and toString().

200

When comparing two objects of a class can I use "==" to reliably do so? Why or why not? If no what must we use instead?

No you can not use "==" because that compares memory location and two objects can be equal without having equal memory locations. 

Instead you must use the equals method defined in the class in order to compare two objects.

 

300

Java variable names are case sensitive? 

True or False

True.

300

Do-while and while loops both have the potential to run x times, what is the minimum number of times that each could run? 

Do-while runs at least once.

While has the potential to never run if the boolean statement is false.

300

String is a primitive type variable.

True or False.

False, it is a class.

300

Within the below switch statement if my case = 1 will my default print out its statement? Why? Or why not?

switch(case) {

    case 1:

        break;

    default: 

        System.out.println("Hoopla");

        break;

No, since my case is equal to 1 is taken into account my switches default case will never be reached.

300

What is NumberFormat most commonly used for?

Money (Currency) Formatting

300

If a child's class constructor makes use of the super constructor, does it need to be the first or last thing called?

First

300

What would the subscript/indices be for an array of size 10?

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

300

What is the difference between a static and an instance variable?

Each object of a class has its own copy of each instance variable, but only a single copy of each static variable is shared by all objects.

300

Write a for loop that will allow you to iterate through a the following array array and insert a 1 at every index. 

int[ ] myGrid = new int[5];

for(int i = 0; i < myGrid.length; i++) {

    myGrid[i] = 1;

400

Declare an integer named highScore and initialize with the value of 200.

int highScore = 200;

400

Is this a legal use of two for loops:

for(int i = 0; i < 5; i++) 

{

    for(int j = 0; j < 5; j++)                                 

    {

    }

}

Yes

400

String name = "Joe";

int age = 26;

Output the name variable with the age added to it within one print line. (Hint: Use concatenation)

System.out.println(name + " " + age);

400

What is && mean? What about || ? Where are these used?

&& means and || means or. These are used in combining two or more boolean expressions.


400

When using the DecimalFormat class what is missing below:

DecimalFormat myFormat = new DecimalFormat(???)

A String Pattern.

400

Where would you see the final modifier used? What does it do?

You will see it used with instance variables, methods and classes.

It indicates that the variable, or method can not be redefined. If used with a class it indicates that the class can not be used as a base/derived class.

400

An array with an array within each index is called a what?

Multidimensional array

Specifically a two dimensional in this case.

400

Math is a static class within java. If I want to use its method random() in order to generate a random number what is the syntax required to do so?

Math.random();

//If I want to store the returned number then must use some variation of below: 

double randomNumber = Math.random();

400

How do I retrieve an int from a String?

Assuming our String is named "theString".

Using static parseInt method from Integer class.


Integer.parseInt(theString);

500

The header for your main method is:

public static void main(String[] args)

500

A for statement is broken into three parts, those being?

for(??? ; ??? ; ???)

Start, End and Increment.

500

String class has all of the following methods except: 

length()                                      charAt()

equals(Other_String)                   nextLine()

toLowerCase()                            indexOf()

nextLine() - This is a Scanner method.

500

What the heck is lexographical order used for?

It is a way to compare two strings. 



Uses ASCII ordering to assign a value to each character in the alphabet, see Appendix 3.

500

double price = 19.8;

Using a DecimalFormat object how would I format my price to include two decimal points and a dollar sign?

Assume DecimalFormat is not imported.

import java.text.DecimalFormat;


DecimalFormat twoDP = new DecimalFormat("0.00");


System.out.println("$" + twoDP.format(price));

500

What is overriding? In the parent-child class relationship where does it take place?

Overriding is when you rewrite the method that has been inherited from your parent. However you have to be sure that the method signature remains the same. 

What happens if the method signature is different? Is it truly overriding or is it doing something else?

500

What is the one instance variable for an array? 

What does it store?

Length and it stores the number of indices within the array.

500

If you do not create an equals and toString methods within a class you write what will be compared when calling the equals method? 


What will be returned if you call the toString method? 

The memory location of the objects will be compared rather then every variable inside (You should check all variables, not memory location).


The memory address will be returned as a String that will be a hexadecimal representation of the memory address.


500
What is a ternary operator? 


Can you write one out?

It is a mini if/else statement that is embeddable within other functions. For example within a System.out.println(). 

(condition) ? trueValue : falseValue;

(i < 0) ? i += 2 : i++;

M
e
n
u