Test 1 Flashcards

1
Q

What is the first part of any java class?

A
public class classname {
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Does indenting matter in Java?

A

No, but it is used to improve human readibility

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the first thing you have to put into any java class?

A

the main method.
public static void main(String[] args){
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a function called in java?

A

A Function is referred to as a method in Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you create a single line comment in java?

A

//

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you create a multi line comment in java?

A
/*
 *
 */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you create a javadoc?

A

/**
*
**/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what needs to go at the end of every line of code in java that doesn’t have a }?

A

;

similar to CSS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In java, What has to be stated for creating variables?

A

the variable has to have their data type declared, unlike python.
int x = 5 ** the variable x is now an integer type variable and can never be changed to a different data type**

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Whats the first two things that needs to be done when dealing with user input in java?

A
You have to import the scanner utility by typing:
import java.util.Scanner
then 
Scanner input = new Scanner(System.in);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are some of the methods used to use the scanner utility?

A

nextInt() and nextDouble() for numeric input, and next() to get a single word or nextLine() to get an entire line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you use the equivalent to the print function from python in java?

A

System.out.println()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The single quote and double quotes are interchangeable in python, is that the case for Java?

A

No, the single quotes are used for the char datatype and represent one character, and the double quotes are used for strings and represent multiple characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the two forms of data types in java?

A

Primitive and Object(reference)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Primitive data type?

A

primitive data types represent a single value, that has no attributes or methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an Object(reference) data type?

A

Object data types represent chunks of data (fields) with code attached (methods)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you do integer division in java?

A

/ not like // in python.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you do the power of in java?

A

with the module Math.pow()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you convert types in java? and what is it called?

A

To convert types in java, you put the name in brackets in front of the expression. This is known as type casting, or casting for short. (int) 5.0 …… makes it 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

When creating a method, do you have to pass the type?

A

You only have to declare the type if the method is returning that type. ex. public int myMethod(){ } ……..
if there is no return then you simply replace the int above with void

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

in the method:
public void myMethod(){
}
what does the void indicate?

A

The void indicates that the method does not return any value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does the GET method do?

A

It returns something back to the class that is calling the method. so make sure you declare the proper data type that is being returned. (Read Access)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What does the SET method do?

A

it passes whatever parameter you are setting & defines a variable with no return. use void since there is nothing being returned. (Write Access)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Do constructors have a pass type?

A

No just leave it blank,, no void, not data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What happens if a set method is made private?

A

only other methods can call upon that method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Whats the difference between methods and constructors?

A

Constructors are defined as they are initiated and cannot be called upon later. Methods can be used as a tool later on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How do you know when a method should be split into different methods?

A

When you can clearly see different parts of the method that could easily act on their own.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

when do you want to make constant variable and how do you do it?

A

if you never want it to be changed again, and you use the final method ex private final double b;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the difference between these two statements?

c1.getB();

System.out.println(c1.getB());

A

the first one returns the location in memory of the object. ex. examReviewClass@5e2de80c

the second one prints the value inside of that variable in the method. ex. 4.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What does the underlined items in a UML diagram mean?

A

it means they are static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

what does static mean?

A

it can be accessed by the other classes without first creating an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

what is the naming convention of a constructor?

A

it has to have the exact same name as the class it is in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

what is overloading a constructor and how does it work?

A

Overloading a constructor is basically building a bunch of different constructors with different possible scenarios for handling input. It allows someone to use the constructor and pass no parameters, or pass different types of parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

how do you check if two methods are equal?

A

you have to build a third method designed to check if both methods are equal.

35
Q

if two method prove true when checking with == what does that mean?

A

they are aliases and both are pointing to the same object.

36
Q

if typing an integer but wanting to save into a long what needs to be done?

A

you need to put a lower case L on the end of the number, or cast it.

37
Q

if typing a double but wanting to save it as a float what needs to be done?

A

you need to put a lower case f on the end of the number, or cast it.

38
Q

what are the only two primitives that you don’t have to cast or put a special symbol on the end?

A

integers & doubles

39
Q

what is wrong with this piece of code?

(int)Math.random() * 5

A

this is going to always be 0 because the math.random gets casted into an int first, which truncates the decimal point and on.

So you have to bracket the random first. ex.
(int)(Math.random() * 5)

40
Q

what is the shortcut for public static void main?

A

psvm + tab

41
Q

what is the shortcut for System.out.println() ?

A

sout + tab

42
Q

how do you check if two string are equal?

A

string1.equals(string2);

43
Q

how does automatic casting work when dealing with integers, doubles, and strings?

A

int + double returns double, string + anything = string

44
Q

whats the one arithmetic operation java does not support for strings?

A

*

45
Q

What is a package?

A

a library of classes

46
Q

Explain how objects work.

A

An object has characteristics, or ATTRIBUTES.
The Values of an object’s attributes with the object a STATE. The actions that an object can take are called behaviours or METHODS.

47
Q

What is encapsulation?

A

Hides the fine detail of what is inside the capsule, only lets the other programmers know how to use it, not how it actually works.

48
Q

What is polymorphism?

A

Polymorphism - Allows the same program instruction to mean different things in different contexts. For example, a method named showOutput might display the data in an object. But the number of data items it displays and their format depend on the kind of object that carries out the action.

49
Q

What is inheritance?

A

Inheritance - a way of organizing classes. You can define common attributes and behaviors once and have them apply to a whole collection of classes. By defining a general class, you can use inheritance later to define specialized classes that add to or revise the details of the general class.

50
Q

What is the name of a variable technically called in java?

A

an identifier

51
Q

What is the naming convention for class names?

A

The names of class variables start with a capital and continue camelCasing : ClassVar

52
Q

What is the naming convention for primitives?

A

The names of a primitive variable start with a lower case the camelCasing: primVar

53
Q

What is the naming convention for constants?

A

All capitals and underscores.

DAYS_PER_WEEK

54
Q

How do you declare a constant variable?

A

with the final method:

Public static final Int DAYS_PER_WEEK = 7;

55
Q

What is the incremental operator?

A

++ (adds 1)
If before, the increment or decrement is done first then the variable is given
If after, the variable is given and then the increment or decrement is done to the variable

56
Q

what is the Decremental operator?

A

– (subtracts 1)
If before, the increment or decrement is done first then the variable is given
If after, the variable is given and then the increment or decrement is done to the variable

57
Q

what is the difference between initializing and declaring a variable?

A

Initialize means creating first time, declaring means assigning to current variable.

Initialize & Declare: int i = 0;
Initialize: int i;
Declare: i = 1;

58
Q

What is the difference between a while loop, and a do while loop?

A

While: while something is true, execute block of code until condition is not true

Do: executes the loop one time no matter once, then checks condition to see if continue.

So while loops execute the block of code from 0 to infinite, where Do while loops at least execute block of code once.

59
Q

If the scanner function is not acting as you expected what is the first thing you should try?

A

Flushing the scanner, by calling an empty scanner method before the one acting weird.

60
Q

when do you not need to import a class or package?

A

if it is in the same local folder the current class is in.

61
Q

when do you need to use the this. method in constructors?

A

when the arguments used in the constructor have the same variable name as the class it is in.

62
Q

How do you negate a boolean expression?

A

by putting ! infront of the boolean expression surrounded by brackets. ex.
(!(x > y)) or (!(variable))

63
Q

can you use non static variable in a static method?

A

No

64
Q

What is another way to manipulate strings?

A

StringBuilder Class Object

or import java.io.*;

65
Q

in a nested if-else statement, what do the else statements correlate to the if statements?

A

In an if-else statement, each else is paired with the nearest preceding unmatched if unless brackets are used for control flow

66
Q

What is the proper way to build out a multi-branch nested if statement?

A
if (balance > 0)
   System.out.println("Positive balance");
else if (balance < 0)
   System.out.println("Negative balance");
else if (balance == 0)
   System.out.println("Zero balance");
67
Q

what is the exit method and how do you use it?

A

Sometimes your program can encounter a situation that makes continuing execution pointless. In such cases, you can end your program by calling the exit method, as follows:

System.exit(0);

68
Q
if variable b is a boolean with a value of true, which is correct?
if (b){}
or
if (b == true){}
A

both are completely valid

69
Q

What is short circuit evaluation when dealing with boolean statements?

A

Java evaluates the first subexpression, and if that is enough information to learn the value of the whole expression, it does not evaluate subsequent subexpressions.

70
Q

how is a switch statement built out?

A
switch (eggGrade)
{
   case ‘A’:
   case ‘a’:
      System.out.println("Grade A");
      break;
   case ‘C’:
   case ‘c’:
      System.out.println("Grade C");
      break;
   default:
      System.out.println("We only buy grade A and grade C."); 
      break;
}

Sometimes you want a case without a break statement if you want it to run more than one

71
Q

how is the do while loop structured?

A
do 
{
     First_Statement
     Second_Statement
       . . .
     Last_Statement 
} while (Boolean_Expression);
72
Q

in the for loops opening statement what are the three sections needed?

A

where to start, where to end, and how much to increment.

ex. for (int counter = 1 ; counter < 10 ; counter++)

73
Q

can you use break statements in loops?

A

yes if the program calls for it, just like in nested if statements. should be avoided if possible

74
Q

what is the difference between a break statement and a continue statement in a loop?

A

the break statement ends the loop, the continue statement just ends that current iteration of the loop and starts the next iteration. should both be avoided in all loops if possible

75
Q

how do you build a getter?

A

public String getBreed(){
return breed;
}

we chose string but that could have been int if the datatype was an int etc.

76
Q

how do you build a setter?

A

public void setBreed(String b){
breed = b;
}

setters are always void because they never return a value

77
Q

how do you call on a getter?

A

System.out.print(dog1.getBreed());

78
Q

how do you call on a setter?

A

dog1.setBreed(pug);

79
Q

when do you use the .this method?

A

when your constructors are using the same name as variables already used in the class. If you wanted to avoid this you could simply use a different variable name for the constructor.

80
Q

when creating a class what extra step do you need to take when creating variables?

A

public int a;

the public needs to be added

81
Q

what is the scope of a variable?

A

within its curly braces

82
Q

how are individual classes saved?

A

to individual files ending in .java

83
Q

what are the two kinds of methods?

A

Java has two kinds of methods: those that return a single value or object and those that perform some action other than returning an item.

84
Q

can a method that returns also perform an action?

A

absolutely, as long as it is returning a value at the end.