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
What is overloading?
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); } ```
26
What is the signature of a method?
its name and argument list computeArea(double myRadius)
27
What are the 5 conditions in Java?
If Else Else if ==, not
28
How do you write comments?
// ``` /* */ ```
29
In conditions, what does == mean?
equals
30
In conditions, what does != mean?
not equals
31
>=
greater than or equal to
32
<=
less than or equal to
33
How do you string operations together?
&& (both conditions have to be true - and) || (either condition has to be true - or)
34
When conducting string equality, how should we compare two strings?
if (a.equals(b)) {... } DO NOT DO: if(a==b){...}
35
What questions can you ask yourself to develop a good testing mindset?
How can I convince myself that this programme works? What are good inputs to check (test cases)?
36
The import command
imports a class e.g. Scanner import.util.Scanner;
37
System.in
gets text entry from the user's keyboard
38
System.in.read()
a method that calls the System.in
39
nextLine()
collects all key presses and returns them into a String
40
nextLine()
collects all key presses and returns them into a String
41
What does \ denote?
A special character
42
\t
tab
43
\n
newline
44
Java shortcuts: int i = 0; System.out.println(i); i++; What does i++ do?
increases i by 1 each time
45
What are the 3 types of Loops java uses?
do Loops while Loops for Loops
46
What is a loop?
a compound statement that keeps repeating until some condition is met. Scope rules apply!
47
how do we start writing a do Loop?
``` do{ //some conditions } while(condition); ```
48
if you declare a variable within it a loop, will it be visible outside the loop?
No But loops can see variables declared before the loop!
49
how do we make a while loop?
``` while(condition){ //some condition } ```
50
what is the difference between a do loop and a while loop?
do loop - checks the condition at the end while loop - checks the condition first
51
What is a counter loop and can you give an example of a type of look that does this?
loops that change some variable at each iteration (e.g. i++) for loops
52
What is a counter loop and can you give an example of a type of look that does this?
loops that change some variable at each iteration (e.g. i++) for loops
53
What is the syntax for FOR loops?
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
What does this mean? for(int i=0; i<=9; i++)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | count up
55
for(int i=9; i>=0; i--)
9, 8, 7, 6, 5, 4, 3, 2, 1, 0 | count down
56
for(int i=0; i>=9; i+=4)
0, 4, 8
57
int i=1;i<=20;i*=5
1, 5
58
for(int i=0; i>=20; i*=5)
0000000 until programme ends
59
for (;;) | ?
infinite loop
60
What are the two loop commands?
break continue
61
what is a break?
jumps out of the loop break;
62
what is continue in a loop?
jumps straight to the next iteration (if there is one) e.g. prints only even numbers
63
String.format?
a method that allows us to combine Strings and numbers with control over: - length - padding (with spaces) - number of digits after decimal point
64
String line = String.format("%15s, %5d", name, age) What does %15s mean? What does %5d mean?
%15s string of 15 characters long. No truncation. %5d pads the integer to be total 5 characters
65
%d
gets the integer as is
66
%5d
pads the integer with spaces to give a length of at least 5
67
%-5d
adds padding after the integer 23---
68
%05d
pads with (0) instead of spaces 00023
69
How do you string.format doubles?
%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
%7.2f
---3.14
71
%7.3f
--3.142
72
integers: | %05.2f
pads with 0 rather than space
73
integers: | %-5.2f
does the padding after the number
74
What does String[] args denote?
a placeholder for information we want to pass into our program.
75
What does System.out.println() denote?
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
What does System.out.print(). denote?
this type of print statement outputs everything on the same line
77
What does the {} denote
the scope of our classes and methods
78
What does it mean when we say Java is a compiled programming language?
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
ava programs have at least one class and one main() method. What does the class mean? What does the main() mean?
Each class represents one real-world idea. The main() method runs the tasks of the program.
80
What are the three primitive data types?
int double boolean
81
What can char hold?
any character, like a letter, space, or punctuation mark must be surrounded by single quotes ''
82
Is String an object?
YES
83
How do we use a String literal? How do we call a String class to create a String object?
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
What are the 3 escape sequences?
``` \" 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
What does static typicing mean?
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.