Introduction to Java Flashcards

You may prefer our related Brainscape-certified flashcards:
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
Q

Compile time errors occur when…

A

The program is fundamentally flawed (e.g. syntactically incorrect), hence an executable version of the program can not be compuled

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

Run time errors occur when…

A

A problem arises halfway through the execution of the program, e.g. divide by 0

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

Logical errors occur when…

A

There’s no compile time or runtime errors (i.e. the program runs perfectly) but does not produce the intended results

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

A class is…

A

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

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

A method is…

A

a collection of statements that performs a sequence of actions to return a message (or void)

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

The main method…

A

provides the control of program flow

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

The java interpreter executes the application by invoking the..

A

main method

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

The main method looks like…

A
public static void main(String[] args) {
   //Statements;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

An algorithm is…

A

a set of precise instructions that lead to a solution

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

An algorithm does not..

A

have to be written in programming language, it can be written in pseudocode (a mix of human and programming languages)

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

Pseudocode must be…

A

precise and clear enough so that a programmer can convert it into syntactically correct code easily

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

Pseudocode is much less…

A

rigid than programming language

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

The steps to solving a problem are…

A
  1. Understand the problem
  2. Design a solution
  3. Consider alternatives and refine the solution
  4. Implement
  5. Test
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

The steps to solving a problem are…

A

not linear and overlap

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

The key to designing a solution is…

A

breaking it down into manageable pieces with classes and objects (i.e. modular programming)

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

What type of programming language is Java?

A

OOP Object-Oriented Programming Language

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

The Scanner class (type) is a template for…

A

creating many Scanner objects

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

objects are

A

an entity that combines data (variables OF the object) and behaviour (methods OF the object)

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

Object-oriented programming is

A

program that performs their behaviour as interactions between objects

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

How is an object constructed?

A

Type objectName = new Type (parameters);

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

Calling an object’s method:

A

objectName.methodName (parameters);

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

A variable holds either:

A
  1. Primitive types

2. Reference to objects

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

To define an OBJECT REFERENCE variable,

A

a class name can be used a the type

e.g. String name;

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

The declaration String title;

A

does not create a new object, the object is constructed separately or constructed when it is assigned

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

An object reference variable holds the…

A

address of an object

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

title = new String (“Java”);

is…

A

an example of

  1. a constructor method
  2. instantiation (an instance of a particular class is created which is an object)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

Once an object is created, the dot operator is used to…

e.g. count = title.length();

A

invoke a method

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

The act of assignment…

e.g. num1 = num2

A

takes a COPY of a value/address and stores it in a variable, nothing is destroyed

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

Aliases are…

A

two or more object reference variables that refer to the same object, they are the alias of each other

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

An object is a garbage when…

A

It is no longer being referenced, therefore becomes useless

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

Java performs garbage collection…

A

automatically and periodically

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

To create a new String object, the constructor method is_______ because ________.

A

not required because it is so common

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

String literals each represent…

A

a String object

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

Literal strings are defined by…

A

enclosing in double quotes “String”

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

char firstInitial = “Robert”.charAt(0);

calls a method on…

A

a literal string

60
Q

The last index of a string is…

A

length - 1

61
Q

Does the following declaration create an empty string?

String str;

A

No, str contains a null

62
Q

String str = new String(); is an example of…

and it instantiates a…

A

Constructor method + instantiation

Empty string

63
Q

Once a String object has been creates, its value and its length cannot…

A

be changed

64
Q

An object of the String class is

A

immutable

65
Q

List 10 methods of the String class

A
length();
substring();
charAt();
concat();
indexOf();
lastIndexOf();
equals();
trim();
replace();
toUpperCase();
toLowerCase();
66
Q

Does word1 = word1.toUpperCase(); destroy any objects?

A

No, the variable’s reference is changed to another object.

67
Q

What are the three ways that convert a number into a String object?

A
  1. String str = “” + num;
  2. String str = integer.toString(i);
  3. String str = String.valueOf(i);

where i is an integer

68
Q

What is the difference between the next and nextLine methods of a Scanner object?

A

next - makes String out of a word

nextLine - makes String out of a LINE

69
Q

What does flow of control refer to?

A

branching and looping mechanisms

70
Q

What are the 3 branching mechanisms in Java?

A

if
if-else
switch

71
Q

What are the 3 types of loops in Java?

A

while
do-while
for

72
Q

Most branching and looping are controlled by…

A

boolean expressions

73
Q

What do Decision-Making Statements do?

A

Determine whether a condition is true, and take some action based on determination

74
Q

List 9 logical operators in Java

A

==
&&
||
>=

75
Q

Conditional operator…

A

provides a shortcut to writing an if-else statement

76
Q

Structure

A

variable = (expression) ? value1 : value2;

77
Q

switch statements are…

A

multiple-way if statements

78
Q

How are switch statements structured?

A
switch (variable)
{
case 'X':
   variable2 = expression;
   break;
case 'Y':
   variable2 = expression2;
   break;
default:
   variable2 = 420;
}
79
Q

A compound statement is …

A

a branch statement that is made up of more than one statement

80
Q

A compound statement must be enclosed by

A
{
//curly brackets/braces
}
81
Q

How are multiway if-else statements structured?

A
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
Q

How is a branch statement in a switch statement structured?

A

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
Q

The default branch of a switch statement is compulsory. T or F?

A

False, it is optional

84
Q

What is a boolean expression?

A

An expression that is either true or false. (involves logical operators)

85
Q

Give two examples of a boolean expression.

A

var == var2

var

86
Q

If the == operator is used on Strings (or any object), what is the output of the boolean expression? Why?

A

False. Because it tests whether the two objects being compared have the same memory location rather than same value.

87
Q

Lexicographic ordering is…

A

ASCII Ordering

88
Q

In ASCII ordering, upper case letters…

A

come before lower case

89
Q

In the following declaration where s1 and s2 are two strings,
s1.compareTo(s2)

if a negative number is returned then…

A

S1 comes before S2 in ASCII ordering

90
Q

In the following declaration where s1 and s2 are two strings,
s1.compareTo(s2)

if a positive number is returned then…

A

S2 comes before S1 in ASCII ordering

91
Q

In the following declaration where s1 and s2 are two strings,
s1.compareTo(s2)

if 0 is returned then…

A

S2 and S1 are the same

92
Q
s1 = a
s2 = A

Does s1.compareTo(s2) return a negative number or a positive number? How can we get it to return 0?

A

Positive

by using compareToIgnoreCase

93
Q

A boolean expression can be negated by…

A

Placing ! infront of the boolean expression in brackets

e.g.

!(a>b)

94
Q

What is the difference between & and &&?

A

&& is short circuit

& is not

95
Q

Is the short circuit “and” or “or” operator used more often than non short circuit?

A

Yes

96
Q

What do loops do?

A

Repeated execution of one or a sequence of statements until a terminating condition is reached

97
Q

What are definite loops?

A

A loop that executes for a known number of times. e.g. print the first n prime numbers

98
Q

What are indefinite loops?

A

A loop that executes for an unknown number of times e.g. prompting the user to enter “u wot m8”

99
Q

What are post test loops?

A

Tests terminating conditions at the end of loop instead of start, hence forcing the branch statement to be executed at least once

100
Q

What is the code that is repeated/executed in a loop called?

A

The body

101
Q

What are infinite loops?

A

Loops are infinite when a terminating condition will never be met.

102
Q

What does the continue statement do?

A

Ends the current iteration of the nearest enclosing loop statement

103
Q

What will the following statement do if someIdentifier is an identifier of a loop?

break someIdentifier;

A

Exits the labelled loop even if it isn’t the innermost loop

104
Q

What dos the following do?

someIdentifier:for(int i = 0; i

A

Labels a for loop with the label “someIdentifier”

105
Q

System.exit(0);

does…

A

Ends the entire program.

106
Q

The two most common kinds of loop errors are …

A

unintended infinite loops and off-by-one errors

107
Q

An assertion is…

A

a sentence that says (asserts) something about the state of a program

108
Q

An assertion must be either..

A

true or false

109
Q

An iterator is…

A

an object that allows you to process a collection of items one at a time e.g. a Scanner object

110
Q

What does the next method of an iterator do?

A

returns the next item

111
Q

What does the hasNext method of an iterator do?

A

returns true if there’s at least one more item to be processed

112
Q

If the condition of a for loop is left out, the loop will be….

A

Always true and infinite

113
Q

System.out.println(); does?

A

Start a new line

114
Q

An array is…

A

an object containing a list of elements of the same data type

115
Q

How is an initialiser structured for a new array?

A

type[] name = {1, 2, 3, 4, 5, 6};

116
Q

short[ ] oldValues = {10, 100, 200, 300};
short[ ] newValues = new short[4];

newValues = oldValues;

Is a new array created? (in addition to the blank array)

A

No. The oldValues addresses are copied but not the object itself.

117
Q

How do you duplicate an array object?

A

Copying individual values

118
Q

When an array is passed as an argument in a method, is the array object copied?

A

No, only the address is. But the method can modify the content of the array.

119
Q

How can you compare two arrays?

A

The only way is by comparing the individual values.

120
Q

What is a good way to find the highest value of an array?

A

Create a variable that holds a copy of the highest value encountered so far in the array.

121
Q

Can a method return an array?

A

Yes but it really is the address of the array that is returned.

122
Q

Which two types of declarations does a class contain?

A

Data (static and instance variables)

Method (behaviours)

123
Q

Each method is defined within…

A

a specific class

124
Q

Mechanics of the Method-Calling Process

A
  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
Q

What is method overloading?

A

When more than one method of the same name is defined. e.g. println

126
Q

How does java know which version of an overloaded method is to be invoked?

A

By analysing the signature (i.e. the parameters) and matching them with the correct version

127
Q

What if more than one overloaded method matches the signature?

A

Java picks the best match.

128
Q

What if no method matches the signature?

A

If some implicit conversion can be done to match a method, then the method is invoke with implicit conversion.

129
Q

Ints can become doubles automatically but can doubles be ints?

A

No

130
Q

What are the two types of scopes in Java?

A

class scope and block scope

131
Q

If a variable has a class scope, can it be accessed from within a method?

A

Yes. static and instance variables of the class

132
Q

Which three types of variables are accessible from within a method?

A
  1. class and instance variables
  2. local variables (block scope)
  3. formal arguments
133
Q
static int min (int num1, int num2)
{
//body
}

Are num1 and num2 formal arguments?

A

Yes.

134
Q

What are actual arguments?

A

The parameters that are copied to the formal arguments in a method.

135
Q

What are the two types of Parameter Passing?

A

Call by value

Call by reference

136
Q

Describe the effects of parameter passing by call by value

A

The parameter is copied over, the actual argument cannot be altered from within the method.

137
Q

Describe the effects of parameter passing by call by reference

A

The address is copied over, the actual argument can be altered from within the method since it has access to the actual object.

138
Q

List four types of methods.

A
  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
Q

List four properties of a constructor method

A
-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
Q

What are two things that an object has?

A

state - descriptive characteristics

behaviours - what it can do (or be done to it), may depend on the state, and can change the state

141
Q

What are two types of modules in Java?

A

Classes

Methods

142
Q

What is the process of invoking a method called?

A

Method call

143
Q

What are 3 benefits of modularising programs?

A
  1. Makes program development more manageable
  2. Software reusability
  3. Avoid repeating code
144
Q

What are the steps to evaluating

System.out.println( Math.sqrt( 4 ) );

?

A

Math.sqrt(4) is evaluated first

Then System.out.println is evaluated

145
Q

What is an escape sequence?

A

An escape sequence is a sequence of two characters beginning with the character \