Procedural Programming Flashcards

Youcheng Sun

1
Q

What is a computer program?

A

Collection of instructions that performs specific task when executed by comp, tells comp what to do, step by step. Rep of algorithm. To run it, we need a few things: editor, source file (.java), compiler, class files (.class), virtual machine + library files.

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

What is the case if there are errors at the compilation stage?

A

Compilation can’t take place.

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

What is a .class file?

A

Can be read by any device capable of interpreting/translating file into something it can run. Platform independent so portable. Class file name must match that of Java program you are coding. First letter should be uppercase.

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

What is a virtual machine?

A

Reads + runs .class file.

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

How do you instruct the comp to do something?

A

Use statements, like declaration, assignments or method calls. To instruct to do something multiple times, use loops such as for, while or do. If you want it do something based on a condition, use if else, switch or nested loops.

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

What are the reasons to have the main method as public and typing static + void after it?

A

Public allows everyone to access it so it can be seen + run. Static identifies that it isn’t related to an object + void tells comp that nothing needs to be returned from method.

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

Why do we type String [] args?

A

This relates to parameters the method requires. A string array will be referred to variable name “args”.

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

What is the Scanner used for + how do we access it?

A

Required import from standard Java libraries, java.util, which allows user to input data from console. Import statements must be at top of file. Must set up Scanner variable called userInput so we can store input. Must then set up int/double variable to read this info. We then code nextInt() method which saves input to variable. Must close scanner using userInput.close();

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

What should we do when taking user input?

A

Check user has entered correct data as users easily make mistakes.

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

What is JOptionPane used for + how do we access it?

A

Displays data + allows user input. Must import from Java libraries (import javax.swing.JOptionPane. To show data, use JOptionPane.showMessageDialog(null, “Message”);. To get input, String name = JOptionPane.showInputDialog(“Question: “);

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

What are the 3 steps of programming?

A

Design, commenting in java + coding + compiling + running code. Could encounter errors while compiling + running code so we must return to fix bugs as java is case sensitive.

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

What is a syntax error?

A

AKA compile time error. Not including semicolon, incorrect spelling, leaving out “ or not matching {} or (). The compiler will flag up these errors.

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

What is a runtime error?

A

AKA logic error. Incorrectly spelling statements in quotations, trying to do impossible calculation or referring to undefined value. Harder to trace as compiler won’t flag them up. Some runtime errors are so extreme that they generate an exception e.g. dividing by 0.

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

What is an algorithm?

A

Series of steps which help us to find solution to problem. Must first design on paper. Must be unambiguous, executable + terminating. We use pseudocode before writing actual program and after writing algorithm. Doesn’t have strict requirements as read by humans, not comp.

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

What is a variable?

A

Container to hold info that will change throughout program. Must include name + type when declaring them. Each type has associated size. Beware of overflow. 2 categories of variable types: primitive + non primitive. First letter should be lowercase + not start with nums. Use camel naming method + don’t use reserved words.

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

What is a primitive type?

A

Predefined by language + named by reserved keyword. Don’t share space in memory with other primitive values. 8 types: byte, short, int, long, double, float, boolean + char.

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

What are the 4 integer types?

A

Byte, short, int + long.

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

What are the 2 floating point types?

A

Double + float.

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

What are the 2 non number related primitive types?

A

Boolean (1 bit) + char (2 bytes, 16 bits)

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

Describe double type.

A

All transcendental math functions return double values {-sin;, cos();, sqrt();} Double is best choice when need accuracy over many iterative calcs or when manipulating large nums. To express, e.g. double d1 = 123.4; For scientific notation, double d2 = 1.234e2

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

Describe float type.

A

Faster on some processors (takes up half space as double + imprecise when values very large/small). Useul when need fractional component but don’t require it to be very precise. e.g. fuel price. To express, e.g. float f1 = 123.4f

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

How do you declare char values?

A

Enclose in single quotes, any printable character e.g. char initial = ‘n’ or ‘+’. Anything in unicode.

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

How do you declare a boolean value?

A

No null value, no numeric equivalent. Boolean isHappy = true; Must be lowercase.

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

How do we convert a floating point num to an integer?

A

Using casting. Must be done this way as fractional part is lost + magnitude may be too large so java won’t let you do it normally. e.g. int myInt1 = (int)myDouble1

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

What are some examples of reserved words you can’t use when declaring variables?

A

Any of the data types, catch, continue, extends, final, if, package, super, private, try + while.

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

What is another way of typing -1 or +1 in java?

A

Type variable – or variable ++

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

What happens if you mix integer + floating point variables in an arithmetic expression?

A

You get a floating point number.

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

To find the remainder when dividing, what must we do?

A

Instead of using mod, we use % e.g. 7%4 = 3.

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

How do we find the root of a number?

How do we find the note the power of a number?

A

Math.sqrt(x);

Math.pow (x,n);

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

How do we find the length of a string?

A

int n = stringName.length(); A string of length 0 is empty + denoted by “”.

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

What is a string?

A

A set of unicode characters.

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

How do you return a char value from a string?

A

Use charAt method e.g. char start = name.charAt (0); OR name.charAt(length-1);

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

What is a substring?

A

Allow us to pick out parts of strings e.g. str.substring(start, pastEnd); \n String name = “Hello W”; \n String sub = name.substring (0,5); to give us Hello

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

How can we include a quotation mark in a string?

A

We can use \ e.g. “He said "Hello"”. We can also use backslash for file references + take a new line using \n.

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

What is an exception?

A

An error message from Java Virtual Machine.

36
Q

How might we swap the values of 2 variables?

A

Create a temp variable and place one of the values into it while you swap them. e.g. a=1, b=2, temp=a, a=b, b=temp.

37
Q
How would you code this in java:
b x (1 +r/100)^n
A

b * Math.pow(1+r/100,n)

38
Q

What is the point of selection in java?

A

We need selection to choose between alternative courses of action.

39
Q

What is an if statement?

A

It’s the most basic selection statement, it tells the program to execute a certain code section if a particular thing is true. Ensure to use indentation and curly brackets around the conditional code.

40
Q

What are the 3 logic operators + what can they be used on?

A

And, or + not. And = &&. Or = ||. Not = !. Can be used on boolean + string.

41
Q

How do we compare floating point numbers?

A

We are careful of roundoff errors so find out if they are approximately equal. We test if difference is less than a certain small threshold value, usually called epsilon.
e.g. final double EPSILON = 1E-14;
if (Math.abs (x-y) <= EPSILON) {}

42
Q

How do we test if 2 strings are equal?

A

Use if(string1.equals(string2))

43
Q

How do we compare 2 strings?

A

Compare in lexicographic order. string1.compareTo(string2)

44
Q

When comparing strings, what do the answers we get mean?

A

If the answer is less than 0, string 1 comes before string 2. If the answer is 0, they are equal. If the answer is more than 0, string 2 comes before string 1.

45
Q

What is lexicographic order?

A

Space comes first, then numbers, then uppercase letters, then lowercase letters. Punctuation is found throughout the lexicographic order.

46
Q

What are flowcharts used for?

A

Illustrate structure of decisions + tasks required to solve problems.

47
Q

How do you avoid spaghetti code?

A

Never point an arrow inside another branch.

48
Q

What is a local variable?

A

A variable declared inside a block of an if statement. Can’t be used or seen anywhere else.

49
Q

What is a global variable?

A

Declared outside of an if statement and can be used everywhere, including inside if statements.

50
Q

What are enumeration types?

A

Hold one of finite num of values called enumeration/enum constants. Must be unique. Can only declare one of values held in enum type.
e.g. public ENUM FilingStatus {SINGLE, MARRIED} FilingStatus status = FilingStatus.SINGLE;

51
Q

What is a method?

A

Collection of grouped statements to perform operations. Can be called elsewhere in program. Each consists of method header + body. Can’t define method inside another method. One method can call another method.

52
Q

What are methods for?

A

Reduce code duplication, improve code efficiency + reduce code defects.

53
Q

Give an example of a method declaration.

A

E.g. public static int addTwoNums(int x, int y){int sumTwoNums = x + y; return sumTwoNums;}

54
Q

What are the 3 types of methods?

A

Void method without parameters, void method with parameters + parameterised method with return type.

55
Q

What is a void method without parameters?

A

Executes set of instructions, needs no parameters (info) to work + no expectation to return anything once method executed.

56
Q

What is a void method with parameters?

A

Executes set of instructions using parameters provided, nothing returned once executed.

57
Q

What is a parameterised method with return type?

A

Executes set of instructions using parameters provides, single variable of specified data type returned once method executed.

58
Q

What is a variable’s scope?

A

Where in program it’s visible. Variables defined in diff methods have diff scopes.

59
Q

What is the Java API?

A

Application Programming Interface. Can find out info about standard classes + methods by looking it up.

60
Q

Where should you always put comments?

A

Before all constructors and methods.

61
Q

What are the 3 main parts of loops? (Especially while loops)

A

The test (logical Boolean), the statements to be repeated + how the test will be updated so it isn’t an infinite loop.

62
Q

How do we create for loops?

A

Initialise counter, check condition, execute loop body, update counter + check condition again.

63
Q

What is the difference between for loops + while loops?

A

While loops are event controlled + indefinite but for loops are count controlled + definite.

64
Q

What are do loops for?

A

Execute body of loop at least once before testing condition. e.g. input validation

65
Q

What is a break statement?

A

Terminates a loop, used to break out of loop.

66
Q

What is a continue statement?

A

Jumps to end of current loop iteration.

67
Q

What are sentinel values?

A

If reading sequence of inputs, need to indicate end of sequence using this. E.g. -1.

68
Q

What should you be aware of when computing averages?

A

Ensure count is not 0.

69
Q

How do we compare adjacent values?

A

Use previous.

70
Q

When can we use an array and what is it?

A

If the values are related, we can use an array. This allows us to hold more than one value at a time, it is one variable. It needs a name and a type.

71
Q

How do we initialise an array?

A

When initialising then array, it is good to use a constant rather than a magic number to declare the size.
Each element of the array receives a default value. Primitive = 0, Boolean = false, object = null.

72
Q

How do we add values to fixed length arrays?

A

Arrays are fixed length so can’t add extra values. We can copy it into a new array to fix this. Arrays start at 0 so if it is length 10, then accessing 10th variable won’t work. We can use .length to check number of values.

73
Q

How do we print out the values of an array?

A

We use a for loop to print out all elements of the array.

74
Q

What can arrays be?

A

Arrays can be method arguments or return values. Therefore, if you define a method with an array parameter, you must provide an array as the argument variable.

75
Q

What is true of the size of arrays?

A

An array can’t change size at run time. If you have an array where you need at least 10 and no more than 100, this is going to be a partially filled array so you need a variable to determine how many elements are currently stored in the array.

76
Q

What are parallel arrays?

A

If you want to store data of a different meaning, use parallel arrays e.g. with age + shoe size at same position in diff arrays.

77
Q

What is a 2D array?

A

An array of arrays. E.g. double [] [] entries = new double [7] [3]

78
Q

What does str.split(“ “) do?

A

Breaks given string into array of substrings.

79
Q

What can we do if say 1 student sits 4 exams and another sits 6?

A

Use same method with diff num of arguments e.g. int…values. Values parameter is int [] array containing all arguments that were passed to method.

80
Q

How do you generate a random number?

A

To generate a random number between 0 and 1, use Math.random(). You can use this to fill an array with random values.

81
Q

What is an enhanced for loop?

A

Enhanced for loop is for (typeName variable : collection) {statements;}

82
Q

How do you remove an element in an array?

A

Removing an element in an unordered array can require a little more work, can overwrite element to be removed with last element of array, then decrement variable holding array size. If they’re ordered, must move all elements following element to be removed to lower index + decrement variable holding array size.

83
Q

How do you insert an element into an array?

A

When inserting elements, must ensure array is large enough. Can separate elements when printing by using for (int i = 0; I < values.length; i++) {for (i > 0) {System.out.print(“ | “);}System.out.print(values[i]);} For debugging, System.out.println(“Values = “ + Arrays.toString(values));

84
Q

Why might you use an array list?

A

If your program is collecting input + you don’t know how many elements there will be. They can grow + shrink as required. The ArrayList class supplies methods for common tasks like inserting & removing elements. ArrayList names = new ArrayList(); (must import.java.util.ArrayList)

85
Q

How do we add into an array list?

A

Can’t use primitive types (no int/double). To add, names.add(“Emily”); To get the elements, String name = names.get(2); To set an element to a new value, use names.set(2, “Carolyn”). Can insert element in middle of array list e.g. names.add(1, “Ann”). Moves elements of index 1 + up.

86
Q

How do we remove from an array list?

A

Remove method removes element at given position + moves all elements after it down by one. e.g. names.remove(2); To get the length, use names.size();

87
Q

How do we search for data in an array list?

A

search (index, data)