1214mondaytest Flashcards

1
Q

What are the three main types of loops in java

A

for loop
while loop
do while loop

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

Indeterminate

A

Indeterminate loop situations are those situations where we do not know in advance how many times we may have to repeat (or iterate) the loop body code. (T!)

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

Type of loop favored for indeterminate situations:

A

while or do-while

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

determinate

A

loop situation we know ahead of time exactly how many iterations our loop body code has to iterate.

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

Type of loop favored for determinate situations:

A

Usually we’ll use either a for loop for these situations or we’ll use a counter-controlled while loop.

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

Which loop is a pre-test type of loop?

A

While loop, the loop continuation condition is check first before trying to run the loop body statements

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

How do we prevent an infinite loop?

A

add some code to the loop body that will eventually cause the loop continuation condition to become false

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

sentinel value

A

The sentinel value is a special input value that tests the condition within the while loop. When a sentinel value of zero is entered, the while loop continuation condition becomes false, and the loop terminates or ends.

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

Which type of loop is a post-test type?

A

do-while because it runs the code first and then the LCC is checked after the body code has run.

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

Difference between a while loop and a do while loop?

A

do while loop the loop body statements will always run at least once, because condition is checked after the loop body executes

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

Do-while loop is often used for

A

data validation

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

when is the for loop used?

A

when we know in advance how many times we want the loop to execute. commonly used for arrays

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

For(expression1; expression2; expression3)

A

Expression1 declares and initializes the the loop control variable or loop counter. Some texts call this the initialization action. This part of the loop header runs just once, at the start of the loop. Expression2 is the loopcontinuation condition expression. It is re-evaluated after each iteration of the loop
Expression3 is the loopcounter adjustment expression. (Some call it the action-after-each-iteration). It is the last action done in an iteration of the body code before the loop goes back to check the LCC again.

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

int count = 0; count < 5; count++) ; //semicolon here is error. What happens?

A

(Logic error)

This separates the loop header from the code in the loop body. The two parts then run independently of each other.

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

How do you get out of a loop if a certain condition is met?

A

with the break statement

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

What does the continue keyword do?

A

it will stop further execution of just the current iteration of a loop. (T!)

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

difference between continue and break?

A

continue stops only the current iteration of the loop

break is used to break out of the loop body completely, no matter how many iterations might remain

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

The for : each loop for(datatype: collection_Name)

A

Basically, it allows you to process all the data values stored in the storage structure without having to worry about how many elements there are in the structure ( this saves a few lines of code)It uses what is called an iterator (it’s like an indicator that points to each value in the data structure) to iterate through the values.It does have its usefulness, but it is actually a little less powerful than a standard for loop. In the declaration statement for(datatype: collection_Name) the datatype refers to the type of data that has been stored in the data structure that has ‘collection_Name’ as its identifier

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

Reading From A File Using a Scanner Object

A

If we want the Scanner object to read a file instead of the keyboard buffer, then we create it like this: // first we create a File object that has the path name of the file File myFile = new File(“DataFile_1.txt”); // then we create the Scanner and pass it the File object Scanner fileReader = new Scanner(myFile);

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

A path is:

A

A string argument that indicates the location of a folder (directory) or a file in your computer’s file system.Example: “C:\Temp2” is a path to a folder(directory) on drive ‘C’

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

Absolute path

A

any path string that begins with a drive letter or root prefix is known as an ABSOLUTE path

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

Relative path

A

relative to the current eclipse project directory which the java program is located
C:\MyEclipseWorkspace\FileExamplesProject\output.txt

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

COMMON ERROR:

A

putting the file you want to read in your ‘src’ source folder in your Eclipse project instead of in the project folder itself.
FileNotFoundException

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

File class object in Java is

A

Best described as an abstract representation of file and path names. (T)

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

IMPORTANT NOTE: Creating a File object

A

DOES NOT Create a corresponding file folder on your hard drive. (T!)
We could later actually create the directories on the disk by using methods such as mkdir() and mkdirs().
We can also create an actual empty file using createNewFile() IF THE FOLDERS in the path have been have been created first.

26
Q

What are exceptions

A

objects that the Java virtual Machine creates to signal that something has gone wrong in your program. We say an exception is thrown when it is created by the JVM

27
Q

How can we prevent ArrayIndexOutOfBoundsException?

A

we can prevent this exception by using “ i < array.length ; “ for our loop continuation condition.

28
Q

Why do you get ArithmeticException

A

code tries to do a division by 0

29
Q

FileNotFoundException

A

JVM cannot find the file that you specified in your path string.

30
Q

UNCHECKED flavor:

A

the coder does not have to write any special code called exception handling code

31
Q

Examples of checked exception

A

FileNotFoundException and IOException both of which involve possible file operations.

32
Q

how do you confess

A

by using the throws keyword

33
Q

Confessing using a throws clause will let your program compile properly, but it gives no _________ if the exception is actually thrown. Your program will _____.

A

Protection

crash

34
Q

biggest benefit of the try-catch is

A

after the exception is caught by your catch block, your program keeps running! it does not crash.

35
Q

what is a path

A

a string argument that indicates the location of a folder (directory) or file in your computer’s file system
Example “C:\Temp2\notes.txt” i

36
Q

path string that begins with a drive letter or root prefix is known as an _____ path

A

absolute path (T!) C:\Temp2\notes.txt

37
Q

There is a hasNext

_____() for each primitive type EXCEPT for type char, just as there is not a _______ method in the scanner class

A

nextChar() doesn’t exist

38
Q

True or false: An array is a type of data structure constructed in memory.

A

True

39
Q

Zero-based indexing

A

first cell is always number 0

40
Q

True or false Important Point : Data types may not be mixed in the elements of an array.

A

True (T!)

41
Q

Arrays are _______ with respect to data type. This means all the data values must be of the same type

A

homogeneous

42
Q

Each element has an element index number. First element of an array is always numbered as element ____

A

0 (zero).

43
Q

ARAY INDEX OPERATOR.

A

The operator we use in creating an array is the square brackets [ ],

44
Q

How do we instantiate or build the array

A

we use the new keyword and then specify how many elements we want

45
Q

To create an array to hold ten values of type int.

A

int [] myIntArray = new int [10];

46
Q

True or false: Once created the array size is fixed, or immutables.

A

true, you cannot change the number of elements (boxes) in an array once it is created

47
Q

The last element of any array will have an index number equal to

A

the (number of elements – 1).

48
Q

Create an array of String objects to hold 25 surnames:For an array of int values typed in by user:For an array of double value test scores:For an array of Scanner objects:

A

String[ ] surNameArray = new String [25];
int [ ] userInputsArray = new int [100];
double [ ] testScoresArray = new double [350];
Scanner[ ] scannerArray= new Scanner[3];

49
Q

To enter a value in any element we use an assignment statement and specify what element gets the value.

A

myIntArray[0]= 5;

50
Q

What is the index number of the 10th element?

A

9

51
Q

offers a shortcut way to declare, create and populate an array all at once by enclosing the data values inside curly braces.

A

String [ ] monthsArray = {“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”, ”Nov”,”Dec”};

52
Q

True or false: An array object is the only other type of object besides a String that can be created without using the new keyword.

A

TRUE

53
Q

True or false: When an array is created, Java will automatically assign a default value to each element

A

TRUE

54
Q

Default value or each primitive type in an array of type. Int, Short, Double, float, etc) boolean, char and string

A

primitive data types: 0
boolean = false
char = unicode character ‘u0000’
string will gets NULL assignment to each element

55
Q

How do you guarentee you never have an OutOfBoundsException

A

Use the arrayName.length which holds how many elements it has
for(int count=0; count < arrayName.length; count++)

56
Q

What operator should we use with arrayName.length?

A

< always

<= will set you out of bounds

57
Q

Two tips for array processing using for loops

A

1: initialize your for loop counter to 0. so it doesn’t skip first element
2: always set the LCC to < arrayName.length in order to not get an OutOfBoundsException

58
Q

Math.random( ) returns a double value ranging from

A

0.0000 up to 0.9999999999 it does not return 1.0 (T!)

59
Q

Want math.random to return a value from 0-9? 1-10?

A

times it by 10 then truncated for 0-9

times it by 10+1 then truncate for 1-10

60
Q

To generate int values with a lower end value of “low” and an upper end value of “high”, the general formula is

A

(int)(Math.random() * (highValue – lowValue + 1) + lowValue);

Example: you need to generate random values from 5 to 15 for some game. Using the formula it would look like this: (int)(Math.random() * (15 – 5 + 1) + 5), which reduces to (int)(Math.random() * (11) + 5);