Basics Flashcards

1
Q

How many characters can a long store?

long largest =

A

19 characters

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

Can a comparison in Java have more than 2 sides? e.g.

while(!dice1 == dice2 ==dice3)

// all 3 dice match

?

A

No. Only 2 sides. So:

!(dice1 ==2 && dice2 == dice3)

while not
dice 1 is the same as dice 2
and
dice 2 is the same as dice 3

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

Define a variable

A

a called message (with some rules) that has been stored under a variable name

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

Variable Name rules?

A

1) starts with a lower case letter

2) must define the type (String, Boolean, Double, Int)

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

Variable Name rules?

A

1) starts with a lower case letter

2) must define the type (String, Boolean, Double, Int, Char)

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

Variable Name declaration rules?

A

1) starts with a lower case letter
2) must define the type (String, Boolean, Double, Int, Char)
3) camel case - mutiple words strung together, first word must be lower case followed by upper case on first letter of each subsequent word e.g. glasgowIsgreat = true;
4) cannot start with numbers
5) cannot start with ‘ or “
6) stick to standard text characters
7) make it mean something and simple (as long as you like)

String message = “Hello!”;

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

double?

A

stores decimals, -ve numbers

1,1, 2.1345, -1.4e10

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

char

A

stores individual characters

a, b, 1

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

char

A

stores individual characters
(a, b, 1)
char d = ‘f’;

Not single ‘’

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

boolean

A

stores T or F

boolean GlasgowIsgreat = true;

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

What is camel case?

A

mutiple words strung together, first word must be lower case followed by upper case on first letter of each subsequent word e.g. glasgowIsgreat = true;

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

What is the difference between doing maths using double and int?

A

Integer division - the remainders are left out!

int a = 3;
int b = 2;

a/b;
= 1 [not 1.5!]

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

For integer division, how does Java get the remainder (how much is left)?

A
3 % 2 = 1
// can remove one 2 from 3, and we are left with 1
5 % 6 = 5
// all 5 are remaining

10 % 4 = 2

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

What is the Order of Operations in Java?

A

1) not necessarily Left to Right
2) Order is:
*
/
+
-
* and / are done BEFORE + and -

3) If multiple / and *, they are done Left to Right

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

When using brackets to specify the order of operations, which nested values are evaluated first?

A

the innermost
e.g.
(a(b + c))/(ac + b);

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

If you want to divide two int variables and then do a standard double division i.e. change from int division to answer being a double?

e.g. double c = a/b

A

double c = 1.0*a/b;

1.0* converts a into a double and then the division is done as a double division

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

What is casting?

A
Casting is converting one type to another and storing it in another variable
e.g.
convert d (a double variable) into an int and storing the result in c
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Can you cast from a double to a string?

A

NO

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

What is a literal?

A

when we put actual values into the programme e.g.
String message = “Hola Chica!” –> is the literal

It is a hard coded value

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

What is a method?

A

a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
Can you put two variables inside the same scope, even if it's a different type? e.g.
int a = 5;
a = 6;
or
int a = 5;
int a = 6;
A

NEVER
int a = 5;
a = 6;

is correct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
Can you put two variables inside the same scope, even if it's a different type? e.g.
int a = 5;
a = 6;
or
int a = 5;
int a = 6;
A

NEVER
int a = 5;
a = 6;

is correct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
Can you put two variables inside the same scope, even if it's a different type? e.g.
int a = 5;
a = 6;
or
int a = 5;
int a = 6;
A

NEVER
int a = 5;
a = 6;

is correct

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

How do you stop a programme from running from inside a programme?

What is the convention?

A

System.exit(int n);

Calling this method terminates your programme. The value n is passed back to whatever started your programme and the normal convention is:
N = 0 //sucessfully ran
N = 1 //unsuccessful

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

What is overloading?

A

same name for two different methods but with different arguments

e.g. to calculate the area of a rectangle

public static double rectangleArea (double width){
     return rectangleArea(width, width);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the signature of a method?

A

its name and argument list

computeArea(double myRadius)

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

What are the 5 conditions in Java?

A

If

Else

Else if

==,

not

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

How do you write comments?

A

//

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

In conditions, what does == mean?

A

equals

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

In conditions, what does != mean?

A

not equals

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

> =

A

greater than or equal to

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

<=

A

less than or equal to

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

How do you string operations together?

A

&&
(both conditions have to be true - and)

||
(either condition has to be true - or)

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

When conducting string equality, how should we compare two strings?

A

if (a.equals(b)) {…
}

DO NOT DO:
if(a==b){…}

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

What questions can you ask yourself to develop a good testing mindset?

A

How can I convince myself that this programme works?

What are good inputs to check (test cases)?

36
Q

The import command

A

imports a class e.g. Scanner

import.util.Scanner;

37
Q

System.in

A

gets text entry from the user’s keyboard

38
Q

System.in.read()

A

a method that calls the System.in

39
Q

nextLine()

A

collects all key presses and returns them into a String

40
Q

nextLine()

A

collects all key presses and returns them into a String

41
Q

What does \ denote?

A

A special character

42
Q

\t

A

tab

43
Q

\n

A

newline

44
Q

Java shortcuts:
int i = 0;
System.out.println(i);
i++;

What does i++ do?

A

increases i by 1 each time

45
Q

What are the 3 types of Loops java uses?

A

do Loops
while Loops
for Loops

46
Q

What is a loop?

A

a compound statement that keeps repeating until some condition is met.

Scope rules apply!

47
Q

how do we start writing a do Loop?

A
do{
//some conditions
} while(condition);
48
Q

if you declare a variable within it a loop, will it be visible outside the loop?

A

No

But loops can see variables declared before the loop!

49
Q

how do we make a while loop?

A
while(condition){
//some condition
}
50
Q

what is the difference between a do loop and a while loop?

A

do loop - checks the condition at the end

while loop - checks the condition first

51
Q

What is a counter loop and can you give an example of a type of look that does this?

A

loops that change some variable at each iteration (e.g. i++)

for loops

52
Q

What is a counter loop and can you give an example of a type of look that does this?

A

loops that change some variable at each iteration (e.g. i++)

for loops

53
Q

What is the syntax for FOR loops?

A

for(, , )

variable : to use for the loop and its initialisation (can be declared here or earlier)
e.g. i

condition : has to be true for loop to keep looping

change : how the variable should be changed at each iteration
i++

54
Q

What does this mean?

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

A

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

count up

55
Q

for(int i=9; i>=0; i–)

A

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

count down

56
Q

for(int i=0; i>=9; i+=4)

A

0, 4, 8

57
Q

int i=1;i<=20;i*=5

A

1, 5

58
Q

for(int i=0; i>=20; i*=5)

A

0000000 until programme ends

59
Q

for (;;)

?

A

infinite loop

60
Q

What are the two loop commands?

A

break

continue

61
Q

what is a break?

A

jumps out of the loop

break;

62
Q

what is continue in a loop?

A

jumps straight to the next iteration (if there is one)

e.g. prints only even numbers

63
Q

String.format?

A

a method that allows us to combine Strings and numbers with control over:

  • length
  • padding (with spaces)
  • number of digits after decimal point
64
Q

String line = String.format(“%15s, %5d”, name, age)

What does %15s mean?
What does %5d mean?

A

%15s
string of 15 characters long. No truncation.

%5d
pads the integer to be total 5 characters

65
Q

%d

A

gets the integer as is

66
Q

%5d

A

pads the integer with spaces to give a length of at least 5

67
Q

%-5d

A

adds padding after the integer

23—

68
Q

%05d

A

pads with (0) instead of spaces

00023

69
Q

How do you string.format doubles?

A

%f

e.g.%5.2f

pads to at least 5 with 2 digits after the decimal point
e.g. 3.1415 becomes -3.14
. counts as a character

70
Q

%7.2f

A

—3.14

71
Q

%7.3f

A

–3.142

72
Q

integers:

%05.2f

A

pads with 0 rather than space

73
Q

integers:

%-5.2f

A

does the padding after the number

74
Q

What does String[] args denote?

A

a placeholder for information we want to pass into our program.

75
Q

What does System.out.println() denote?

A

System is a built-in Java class that contains useful tools for our programs.

out is short for “output”.

println is short for “print line”.

76
Q

What does System.out.print(). denote?

A

this type of print statement outputs everything on the same line

77
Q

What does the {} denote

A

the scope of our classes and methods

78
Q

What does it mean when we say Java is a compiled programming language?

A

the code we write in a .java file is transformed into byte code by a compiler before it is executed by the Java Virtual Machine on your computer

A compiler is a program that translates human-friendly programming languages into other programming languages that computers can execute.

The compiling process catches mistakes before the computer runs our code.

79
Q

ava programs have at least one class and one main() method.

What does the class mean?

What does the main() mean?

A

Each class represents one real-world idea.

The main() method runs the tasks of the program.

80
Q

What are the three primitive data types?

A

int
double
boolean

81
Q

What can char hold?

A

any character, like a letter, space, or punctuation mark

must be surrounded by single quotes ‘’

82
Q

Is String an object?

A

YES

83
Q

How do we use a String literal?

How do we call a String class to create a String object?

A

literal:
A String literal is any sequence of characters enclosed in double-quotes (String greeting = “Hello World”;)

class:
create a new String object by calling the String class when declaring a String
(String salutations  = new String ("Hello World!");
84
Q

What are the 3 escape sequences?

A
\"
 allows us to add " to a String value
e.g.
("\"Hello World\"")
// prints 
"Hello World"
\\
allows you to print backlash \
("This is the back slash symbol: \\")
prints 
This is the back slash symbol:\
\n
new line
("Hellow\nWorld!")
prints:
Hello
World!
85
Q

What does static typicing mean?

A

Static typing helps because bugs are caught during programming rather than during execution of the code.

Java programs will not compile if a variable is assigned a value of an incorrect type.