Introduction to Java Flashcards

(145 cards)

1
Q

In the java programming language:

A program is made up of ________ classes

A

one or more

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

In the java programming language:

A program is made up of ________ , methods

A

one or more

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

A method contains

A

program statements

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

Anatomy of a Java Program:

A
Comments
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you structure a class?

A
//comments about the class
public class classHeader
{

//class body

}

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

How do you structure a method?

A
//comments about the class
public class classHeader
{
   public static returnType name(|inputType| tempName)
   {
      //method body
   }

}

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

Comments in a program are called…

They should be included to…

A

inline documentation, explain the purpose of the program and describe processing steps

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

How are comments structured?

A

// this comment runs the end of line

/* this comment runs to the terminating symbol */

/** comment */

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

Identifiers are…

A

the words that a programmer use in a program

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

Identifiers can be made up of…

A

letters, digits, underscore and $

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

Identifiers cannot begin with…

A

a digit

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

Java is case…

A

sensitive

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

White space includes…

A

spaces, blank lines, tabs

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

White spaces are used to…

A

separate words and symbols in a program and make the program more readable along with indentation

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

Extra white spaces are

A

ignored

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

Reserved words called modifiers are used to

A

specify properties of the data, methods and classes and how they can be used

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

private, final, public and static are examples of

A

modifiers

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

A statement…

A

represents an action or a sequence of actions

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

A statement ends in

A

;

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

Blocks can be identified with…

A

{

}

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

The syntax rules of a language define…

A

how we can put together symbols, reserved words, and identifiers to make a valid program

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

The semantics of a program is…

A

what the programmer intends the program to achieve/do

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

A program that is syntactically correct does not mean it is

A

semantically/logically correct

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

The three types of errors in java are

A
  • Compile time errors
  • Run-time errors
  • Logical errors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Compile time errors occur when...
The program is fundamentally flawed (e.g. syntactically incorrect), hence an executable version of the program can not be compuled
26
Run time errors occur when...
A problem arises halfway through the execution of the program, e.g. divide by 0
27
Logical errors occur when...
There's no compile time or runtime errors (i.e. the program runs perfectly) but does not produce the intended results
28
A class is...
a template or blueprint for constructing objects, it is a program entity that represents either 1. a program/module or 2. a type of object
29
A method is...
a collection of statements that performs a sequence of actions to return a message (or void)
30
The main method...
provides the control of program flow
31
The java interpreter executes the application by invoking the..
main method
32
The main method looks like...
``` public static void main(String[] args) { //Statements; } ```
33
An algorithm is...
a set of precise instructions that lead to a solution
34
An algorithm does not..
have to be written in programming language, it can be written in pseudocode (a mix of human and programming languages)
35
Pseudocode must be...
precise and clear enough so that a programmer can convert it into syntactically correct code easily
36
Pseudocode is much less...
rigid than programming language
37
The steps to solving a problem are...
1. Understand the problem 2. Design a solution 3. Consider alternatives and refine the solution 4. Implement 5. Test
38
The steps to solving a problem are...
not linear and overlap
39
The key to designing a solution is...
breaking it down into manageable pieces with classes and objects (i.e. modular programming)
40
What type of programming language is Java?
OOP Object-Oriented Programming Language
41
The Scanner class (type) is a template for...
creating many Scanner objects
42
objects are
an entity that combines data (variables OF the object) and behaviour (methods OF the object)
43
Object-oriented programming is
program that performs their behaviour as interactions between objects
44
How is an object constructed?
Type objectName = new Type (parameters);
45
Calling an object's method:
objectName.methodName (parameters);
46
A variable holds either:
1. Primitive types | 2. Reference to objects
47
To define an OBJECT REFERENCE variable,
a class name can be used a the type e.g. String name;
48
The declaration String title;
does not create a new object, the object is constructed separately or constructed when it is assigned
49
An object reference variable holds the...
address of an object
50
title = new String ("Java"); is...
an example of 1. a constructor method 2. instantiation (an instance of a particular class is created which is an object)
51
Once an object is created, the dot operator is used to... e.g. count = title.length();
invoke a method
52
The act of assignment... e.g. num1 = num2
takes a COPY of a value/address and stores it in a variable, nothing is destroyed
53
Aliases are...
two or more object reference variables that refer to the same object, they are the alias of each other
54
An object is a garbage when...
It is no longer being referenced, therefore becomes useless
55
Java performs garbage collection...
automatically and periodically
56
To create a new String object, the constructor method is_______ because ________.
not required because it is so common
57
String literals each represent...
a String object
58
Literal strings are defined by...
enclosing in double quotes "String"
59
char firstInitial = “Robert”.charAt(0); calls a method on...
a literal string
60
The last index of a string is...
length - 1
61
Does the following declaration create an empty string? String str;
No, str contains a null
62
String str = new String(); is an example of... and it instantiates a...
Constructor method + instantiation Empty string
63
Once a String object has been creates, its value and its length cannot...
be changed
64
An object of the String class is
immutable
65
List 10 methods of the String class
``` length(); substring(); charAt(); concat(); indexOf(); lastIndexOf(); equals(); trim(); replace(); toUpperCase(); toLowerCase(); ```
66
Does word1 = word1.toUpperCase(); destroy any objects?
No, the variable's reference is changed to another object.
67
What are the three ways that convert a number into a String object?
1. String str = "" + num; 2. String str = integer.toString(i); 3. String str = String.valueOf(i); where i is an integer
68
What is the difference between the next and nextLine methods of a Scanner object?
next - makes String out of a word | nextLine - makes String out of a LINE
69
What does flow of control refer to?
branching and looping mechanisms
70
What are the 3 branching mechanisms in Java?
if if-else switch
71
What are the 3 types of loops in Java?
while do-while for
72
Most branching and looping are controlled by...
boolean expressions
73
What do Decision-Making Statements do?
Determine whether a condition is true, and take some action based on determination
74
List 9 logical operators in Java
== && || >=
75
Conditional operator...
provides a shortcut to writing an if-else statement
76
Structure
variable = (expression) ? value1 : value2;
77
switch statements are...
multiple-way if statements
78
How are switch statements structured?
``` switch (variable) { case 'X': variable2 = expression; break; case 'Y': variable2 = expression2; break; default: variable2 = 420; } ```
79
A compound statement is ...
a branch statement that is made up of more than one statement
80
A compound statement must be enclosed by
``` { //curly brackets/braces } ```
81
How are multiway if-else statements structured?
``` if (Boolean_Expression) Statement_1 else if (Boolean_Expression) Statement_2 ... else if (Boolean_Expression_n) Statement_n else Statement_For_All_Other_Possibilities ```
82
How is a branch statement in a switch statement structured?
Starts with the reserved word case, followed by a constant called a case label of the same type as the controlling expression, followed by a colon, and then a sequence of statements and BREAK
83
The default branch of a switch statement is compulsory. T or F?
False, it is optional
84
What is a boolean expression?
An expression that is either true or false. (involves logical operators)
85
Give two examples of a boolean expression.
var == var2 | var
86
If the == operator is used on Strings (or any object), what is the output of the boolean expression? Why?
False. Because it tests whether the two objects being compared have the same memory location rather than same value.
87
Lexicographic ordering is...
ASCII Ordering
88
In ASCII ordering, upper case letters...
come before lower case
89
In the following declaration where s1 and s2 are two strings, s1.compareTo(s2) if a negative number is returned then...
S1 comes before S2 in ASCII ordering
90
In the following declaration where s1 and s2 are two strings, s1.compareTo(s2) if a positive number is returned then...
S2 comes before S1 in ASCII ordering
91
In the following declaration where s1 and s2 are two strings, s1.compareTo(s2) if 0 is returned then...
S2 and S1 are the same
92
``` s1 = a s2 = A ``` Does s1.compareTo(s2) return a negative number or a positive number? How can we get it to return 0?
Positive by using compareToIgnoreCase
93
A boolean expression can be negated by...
Placing ! infront of the boolean expression in brackets e.g. !(a>b)
94
What is the difference between & and &&?
&& is short circuit | & is not
95
Is the short circuit "and" or "or" operator used more often than non short circuit?
Yes
96
What do loops do?
Repeated execution of one or a sequence of statements until a terminating condition is reached
97
What are definite loops?
A loop that executes for a known number of times. e.g. print the first n prime numbers
98
What are indefinite loops?
A loop that executes for an unknown number of times e.g. prompting the user to enter "u wot m8"
99
What are post test loops?
Tests terminating conditions at the end of loop instead of start, hence forcing the branch statement to be executed at least once
100
What is the code that is repeated/executed in a loop called?
The body
101
What are infinite loops?
Loops are infinite when a terminating condition will never be met.
102
What does the continue statement do?
Ends the current iteration of the nearest enclosing loop statement
103
What will the following statement do if someIdentifier is an identifier of a loop? break someIdentifier;
Exits the labelled loop even if it isn't the innermost loop
104
What dos the following do? someIdentifier:for(int i = 0; i
Labels a for loop with the label "someIdentifier"
105
System.exit(0); does...
Ends the entire program.
106
The two most common kinds of loop errors are ...
unintended infinite loops and off-by-one errors
107
An assertion is...
a sentence that says (asserts) something about the state of a program
108
An assertion must be either..
true or false
109
An iterator is...
an object that allows you to process a collection of items one at a time e.g. a Scanner object
110
What does the next method of an iterator do?
returns the next item
111
What does the hasNext method of an iterator do?
returns true if there's at least one more item to be processed
112
If the condition of a for loop is left out, the loop will be....
Always true and infinite
113
System.out.println(); does?
Start a new line
114
An array is...
an object containing a list of elements of the same data type
115
How is an initialiser structured for a new array?
type[] name = {1, 2, 3, 4, 5, 6};
116
short[ ] oldValues = {10, 100, 200, 300}; short[ ] newValues = new short[4]; newValues = oldValues; Is a new array created? (in addition to the blank array)
No. The oldValues addresses are copied but not the object itself.
117
How do you duplicate an array object?
Copying individual values
118
When an array is passed as an argument in a method, is the array object copied?
No, only the address is. But the method can modify the content of the array.
119
How can you compare two arrays?
The only way is by comparing the individual values.
120
What is a good way to find the highest value of an array?
Create a variable that holds a copy of the highest value encountered so far in the array.
121
Can a method return an array?
Yes but it really is the address of the array that is returned.
122
Which two types of declarations does a class contain?
Data (static and instance variables) | Method (behaviours)
123
Each method is defined within...
a specific class
124
Mechanics of the Method-Calling Process
1. Java evaluates the argument expressions in the context of the calling method. 2. Java then copies each argument value into the corresponding parameter variable, which is allocated in a newly assigned region of memory called a stack frame. This assignment follows the order in which the arguments appear: the first argument is copied into the first parameter variable, and so on. 3. Java then evaluates the statements in the method body, using the new stack frame to look up the values of local variables. 4. When Java encounters a return statement, it computes the return value and substitutes that value in place of the call. 5. Java then discards the stack frame for the called method and returns to the caller, continuing from where it left off.
125
What is method overloading?
When more than one method of the same name is defined. e.g. println
126
How does java know which version of an overloaded method is to be invoked?
By analysing the signature (i.e. the parameters) and matching them with the correct version
127
What if more than one overloaded method matches the signature?
Java picks the best match.
128
What if no method matches the signature?
If some implicit conversion can be done to match a method, then the method is invoke with implicit conversion.
129
Ints can become doubles automatically but can doubles be ints?
No
130
What are the two types of scopes in Java?
class scope and block scope
131
If a variable has a class scope, can it be accessed from within a method?
Yes. static and instance variables of the class
132
Which three types of variables are accessible from within a method?
1. class and instance variables 2. local variables (block scope) 3. formal arguments
133
``` static int min (int num1, int num2) { //body } ``` Are num1 and num2 formal arguments?
Yes.
134
What are actual arguments?
The parameters that are copied to the formal arguments in a method.
135
What are the two types of Parameter Passing?
Call by value | Call by reference
136
Describe the effects of parameter passing by call by value
The parameter is copied over, the actual argument cannot be altered from within the method.
137
Describe the effects of parameter passing by call by reference
The address is copied over, the actual argument can be altered from within the method since it has access to the actual object.
138
List four types of methods.
1. access methods : read or display states (or those that can be derived) 2. predicate methods : test the truth of some conditions 3. action methods, e.g., print 4. constructors: a special type of methods
139
List four properties of a constructor method
``` -they have the same name as the class there may be more then one constructor per class (overloaded constructors) -they do not return any value it has no return type, not even void -they initialize objects of the class, using the new construct: -you do not have to define a constructor ```
140
What are two things that an object has?
state - descriptive characteristics | behaviours - what it can do (or be done to it), may depend on the state, and can change the state
141
What are two types of modules in Java?
Classes | Methods
142
What is the process of invoking a method called?
Method call
143
What are 3 benefits of modularising programs?
1. Makes program development more manageable 2. Software reusability 3. Avoid repeating code
144
What are the steps to evaluating System.out.println( Math.sqrt( 4 ) ); ?
Math.sqrt(4) is evaluated first Then System.out.println is evaluated
145
What is an escape sequence?
An escape sequence is a sequence of two characters beginning with the character \