Basics of Java Flashcards

1
Q

What is Java?

A

a programming language that allows you to provide instructions to a computer, and with which you can create computer programs

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

Java compiler

A

translates Java code into machine or computer language so the computer can execute the program

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

How to delineate a single line and multi line comment?

A

single line comment //
multi line comment //

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

How to name the program?

A

class Main {…}

tells Java the name of the program is Main

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

What is the main method?

A

static public void main (String[] args)

this statement is called the main method and should be included in every Java program. Tells the compiler this is the beginning of the Java program. ‘public’ and ‘static’ can be interchanged.

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

Is syntax important in Java?

A

proper syntax is essential or the program will not run.

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

What is an executable statement?

A

code that produces output

statements in Java must end with a semicolon;

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

What does public mean in

static public void main (String[] args)

A

means that any object can use the main method

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

What does static mean in

static public void main (String[] args)

A

means that the main method is a class method, also means you can access this method without having an instance of the class

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

What does void mean in

static public void main (String[] args)

A

tells Java the main method won’t return a value. Other methods in other classes can receive and return values/variables, but main cannot return anything

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

What does main mean in

static public void main (String[] args)

A

the main method is responsible for calling any other methods within the program. This is the first thing the compiler looks for in the program.

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

What does string args in

static public void main (String[] args)

A

sets up the main method to accept parameters, once packaged and distributed users can specify arguments to pass to the main function

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

What is one argument the main method takes?

A

an array of string elements

(String[] args)

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

What does args do in

static public void main (String[] args)

A

holds the actual parameters passed in

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

What are java statements?

A

instructions that tell the programming language what to do

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

What is an assignment statement?

A

assigns a value to a variable

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

What is a Java method?

A

blocks of code that perform a task and can be used by other parts of a computer program such as a declaration statement.

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

What are the core Java statements that end in curly brackets instead or semicolon?

A

if statement

while statement

for loop statement

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

What is the difference between rules and conventions in Java?

A

Rules are hard and fast: your program won’t compile or run if you violate the rule. Conventions are practices that most Java programmers follow.

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

What are keywords?

A

words that have a predefined meaning for the language

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

Rules of naming variables:

A
  1. variables are case sensitive
  2. cannot be Java keywords
  3. main/Main is not a keyword in Java
  4. no spaces in variable names
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Naming conventions for constants:

A
  1. convention: use uppercase with underscore
    final FIRST_NAME = “Sandy”;
  2. rule: constant names cannot be Java keywords
  3. rule: constants cannot have spaces
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Naming convention for classes and interfaces:

A

Capitalized nouns, each word capitalized

Employee, UnionEmployee, Remote

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

Naming convention for methods:

A

lowercase verbs

public void enterData() {}

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

Naming conventions for variables:

A

short and meaningful

int[] counter, double fees;

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

Naming conventions for constants:

A

all uppercase

universe = ALL;

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

Naming conventions for packages:

A

all lowercase letters

java.util.*;

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

Describe the short data type:

A

smallest type at only two bytes (16 bits)

It accepts values from -32,768 to 32,767

it’s signed, meaning it accepts both negative and positive values

short is used more for lower-level programming, such as image processing or working with sound processing

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

Describe the int data type:

A

consumes 32 bits in memory, is a whole number

the valid number range is:
-2,147,483,648 to 2,147,483,647

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

Describe the long data type:

A

64 bits of memory and accepts a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

It’s useful for storing numbers that outgrow the integer data type.

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

Describe float data type:

A

A float data type is a single precision number format that occupies 4 bytes or 32 bits in computer memory

A float data type in Java stores a decimal value with 6-7 total digits of precision. So, for example, 12.12345 can be saved as a float, but 12.123456789 can’t be saved as a float

default value is 0.0f

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

Describe double data type:

A

A double data type is a double precision number format that occupies 8 bytes or 64 bits in the memory of a computer.

The double data type stores decimal values with 15-16 digits of precision

default value is 0.0d

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

Describe the char data type:

A

char is short for character, 16 bits in size, single character, can be specified via unicode ie. 7 7=M or with single quotes ‘M’

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

What is unicode?

A

Unicode is a computer encoding methodology that assigns a unique number for every character.
65=A
66=B
67=C…..

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

Describe the Java string data type:

A

a sequence of single characters, a string is also a class, which means that it has its own methods for working on strings.

Because a string is a class, when we declare a string, we are creating an instance of that class. Therefore, the keyword new is used to create a new instance

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

length()

A

outputs the length of the string in number of characters

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

toUpperCase()
toLowerCase()

A

output string to all upper case or lower case

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

trim()

A

output removes all spaces in the string

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

replace()

A

replace all instances of a certain character sequence with another

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

Java escape codes

A

" double quote
\ backslash
\t tab
\n new line

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

Concatenation

A

adding strings together

String hello = new String(“Hello World!”);
String niceDay = new String(“It’s a nice day!”);
String output = hello + “ “ + niceDay;

output= Hello World! It’s a nice day!

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

Describe Java boolean data type:

A

a primitive data type, true or false, defaults to false

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

What are the Java primitive data types in order from smallest to largest?

A

1 bit - boolean
8 bit - byte
16 bit - char, short
32 bit - int, float
64 bit - long, double

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

What are the two type of data conversion?

A

Implicit conversion- moving a number from a smaller data type into a larger data type (one with more bits of storage), no information will be lost.

byte->short->int->long->float->double

‘char’ and ‘boolean’ data type are not eligible for implicit conversion

Explicit conversion- when choice does not allow, one must move one data type to another type of narrower size(one with fewer bits of storage). Truncation may be necessary. An error will be cast.

A type cast must be used to force Java to move the value explicitly.

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

What is a type cast?

A

A wider data type can be explicitly cast to a narrower type by enclosing the narrower data type name in parentheses and placing it in front of the variable declared with the wider data type.

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

Describe byte data type:

A

8-bit signed Java primitive integer data type. Its range is -128 to 127 (-27 to 27 - 1).

byte type is the smallest integer data type available in Java.

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

In what order does Java perform math operations?

A

The same order of operations as math does

PEMDAS

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

Java math operators:

A

+ addition
- subtraction
* multiplication
/ division
% modulo (returns the remainder of the division between two numbers)

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

What are relational operators?

A

They help us check if a condition is met by answering questions.

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

==

A

checking to see if the two values are equal to each other

careful!!! not using these correctly i.e. = instead of == will result in an assignment error and actually assign a value instead of comparing a value.

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

!=

A

not equal to

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

> ,<

A

less than or greater than

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

> =

A

greater than or equal to

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

<=

A

less than or equal to

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

Boolean operators

A

AND and OR lets us combine these relational operators to ask questions, such as did the employee work more than 40 AND less than 60 hours this week.

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

&&

A

AND

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

||

A

OR

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

What are API’s in Java?

A

API stands for Application Programming Interface. In Java-speak, the APIs are a collection of packages that programmers can import into their programs.

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

What is a package in Java?

A

Inside each package there are interfaces, classes, methods, fields, and constructors.

There is a lot of functionality in these packages.

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

How to input the Scanner class from the Java util package?

A

import java.util.Scanner;

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

What does pseudorandom mean?

A

that the actual bits underneath the hood are not moved around in an equal manner.

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

nextInt(int n)

A

a method that generates a pseudorandom number from the Random class

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

Java if statement:

A

if(Boolean_expression) {
// Anything here will be executed if the expression is true
}

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

What is the correct way to see if the String object s1 equals “green”

A

if(s1.equals(“green”)) { }

Recall that the Java String object has a method called equals(), and you place what you are comparing between the parentheses of that method.

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

What are the three things a Java program should always. have?

A

main method
class
some comments

conditional statement not necessary

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

What is a switch statement in Java?

A

In Java, the switch statement evaluates the expression, and then goes to a position within the switch statement based on that value.

Data types that cannot be evaluated: doubles or floats.

Specific case statements are processed based on that value. If none of the statements match, a default option is required for any fallout.

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

Describe a while loop:

A

A while statement performs an action until a certain criteria is false. This condition uses a boolean. The code will keep on processing as long as that value is true.

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

What is an indefinite loop?

A

A type of while loop where you don’t know when the condition will be true.

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

A while loop runs code as long as the conditions is/are?

A

The loop will continue as long as the value is true; once they are false the condition will stop.

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

Describe a for loop:

A

A for loop is a counting loop, because it repeats a set of instructions for a set number of times. For loops are useful when you know when the condition will be met.

for (initial start point; stop conditon; amount to increment) {
instructions/code here;
}

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

Nested for loops:

A

a for loop within a for loop

nested for loops are usually declared with the help of counter variables, conventionally declared as i, j, k, and so on, for each nested loop

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

For each loop:

A

used mainly for looping through collections

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

How many times will the nested loop run?

for (int i=1; i<=3; i++) {
for (int j=1; j<=i; j++) {
}
}

A

The nested loop will run at least once for each pass through the main loop. When i is 1, j will run once; When i is 2, j will run twice (since it is set to run as many times as the value of i); when i is 3, j will run three times; for a total of six.

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

What is the problem with this code?

for (int i=1; i<=3; i++) {
for (int j=1; i<=3; j++) {
}
}

A

It could run indefinitely

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

Nested while loop:

A

a while statement inside another while statement. In a nested while loop, one iteration of the outer loop is first executed, after which the inner loop is executed. The execution of the inner loop continues till the condition described in the inner loop is satisfied. Once the condition of the inner loop is satisfied, the program moves to th

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

In the code below what gets printed after 2 : 5?

class Main {

public static void main(String args[]) {

int outerLoop = 2;

while(outerLoop <5) {

int innerLoop = 4;

while(innerLoop < 7) {

System.out.println(outerLoop + ': ' + innerLoop);

innerLoop++;

}

outerLoop++;

}

}

}

A

2:6

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

import java.util.*;

public class Main {

public static void main(String args[]) {

int outerLoop = 3;

while (outerLoop < 8) {

int innerLoop = 5;

while(innerLoop < 7) {

if(innerLoop == 6) {

 break;

} else {

 System.out.println(outerLoop + ":" + innerLoop);

 innerLoop++;

}

outerLoop++;

}

}

}

}

A

Here the inner loop breaks at 6. So it prints only till 5. The outer loop starts at 3 and prints 3 : 5. The next iteration of the inner loop is 6, but at this point the inner loop breaks. So the execution goes to the next iteration of the outer loop 4 and the inner loop 5, and prints 4 : 5.

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

In a nested while loop what happens if there is a break in the inner loop?

A

A break statement in an inner loop causes the inner loop to stop execution if the condition in the break statement is satisfied.

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

What are the branching statements in Java?

A

break, continue, return

80
Q

Uses of the break statement:

A

allows the user to return to the main
if some condition is met during processing

81
Q

Uses of the continue statement:

A

If inside a ‘while’, ‘do’, or ‘for’ loop. There is also a break statement within the code. If we DON’T want to stop at that point, but stop the current iteration and continue to the next, we need a continue statement.

82
Q

What does the Java return statement do?

A

The return statement sends control back to a constructor or method, effectively giving the keys back to the original driver.

83
Q

Describe do-while loop:

A

runs while a specific condition is false

do {
//commands
}
while (condition);

84
Q

What happens if there is a possibility that the condition will never be met?

A

you created an infinite loop and will have to terminate the program

85
Q

Examine the following code. What is missing?

do {
//instructions
}while(true)

A

semicolon at the end of the while statement

86
Q

What is an infinite while loop in Java?

A

a set of code that would repeat itself forever, unless the system crashes. At a certain point the program will overflow and fail.

87
Q

Choosing between switch and if-else statements?

A

One common reason for choosing switch over if statements is when there are a large number of possible scenarios.

Switch makes the code a lot more readable and traceable in the debugging process.

One classic example wherein a decision has to be made by checking (testing) an expression based on a range of values or conditions, switch cannot be used. This is because a switch statement can test expressions based on only on single (permissible) primitive data-type value, a single enumerated value, or a single String object.

88
Q

What is an array?

A

an array is simply a sequence of values, where each item is the same data type.

an array is a single object, even though it is made up of a number of pieces.

89
Q

What is the most common way to step through an array?

A

using a for loop

for(int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
//print out each block in the sequence
}

90
Q

A Java array that would have rows and columns is called a _____ array?

A

two-dimensional

91
Q

default array initialization

A

default array initialization

int[] highScores = new int[5];

92
Q

Examine the following code for an array.

long [] phoneNumber;

Write a statement to initialize it:

A

phoneNumber = new long[55];

93
Q

What is a multidimensional array?

A

an array within an array

94
Q

How to create a table of integers that is 5 by 5 and provide the data to fill the table?

A

table = new int [5] [5];

int [][] table = {
{395,830,137,450,949,
446,610,636,818,22,
807,337,187,812,236,
546,772,699,867,216,
340,814,815,88,483}
};
}

95
Q

What is a dynamic array?

A

an array that can grow and shrink as needed.

96
Q

What is an ArrayList?

A

provides several methods for working with arrays, including adding, updating the index/buckets, and removing elements. Instead of the length option, ArrayList has the size feature.

97
Q

What is a constructor?

A

used to initialize an instance of our class

98
Q

Java array length vs size:

A

Length of an array is the number of elements in the array.

Size applies to ArrayList collection object, not Java arrays.

99
Q

What is a matrix?

A

A table of numbers arranged in rows and columns.

100
Q

How to multiply matrices:

A

Multiplying matrices in Java involves the creation of 2-dimensional arrays and nested for loops to traverse through the matrices and complete the math. When multiplying a matrix, you start at the first row of the first matrix, and down the first column of the second, and then repeating for all for the rest of the values. Each pair (row/column) is multiplied and then added to the total for that row/column pair, known as the dot product.

101
Q

In order for matrix multiplication to work, the number of _____ in the first matrix must match the number of _____ in the second matrix.

A

Columns, rows

102
Q

After completing the matrix multiplication, the resulting matrix will have the same number of _____ as the first matrix, and the same number of _____ as the second matrix.

A

Rows, columns

103
Q

When multiplying matrices, we start at the first _____ of the first matrix and the first _____ of the second.

A

row, column

104
Q

What is an ArrayList?

A

an array that we can add and/or subtract from.

105
Q

What does this code do?

ArrayList <String> employees = new ArrayList <String>(5);</String></String>

A

declares an instance of the ArrayList class and calls it employess

106
Q

If it is possible that a get method will try to reach a non-existent index, what should you do?

A

Place the get method within a try statement, and catch the error in a catch statement

107
Q

What is a command line?

A

A command-line interface is a text-based console, such as the Windows command prompt or the Unix/Linux shell window where you type in all your commands.

108
Q

The main method’s parameter, args, allows passing which of the following?

A

Arguments are stored in a string array and can be converted by the main method to whatever data types it expects.

109
Q

When the main method parses the input parameters, it stores the largest numbers first.

A

False, The arguments are stored as strings in the parameter args in the order in which they are passed in.

110
Q

Which of the java commands will compile java files from the command-line prompt?

A

javac

111
Q

The main method’s parameter, args, allows passing what?

A

Multiple arguments separated by space.

112
Q

how to import Java Scanner?

A

import java.util.Scanner;

113
Q

Code to prompt user to enter a number when they did not enter a number?

A

while (!enterQty.hasNextInt()) {
System.out.println(“Enter Integer”);
enterQty.next();
}

114
Q

Examine the following code. What is true about the variable inputValue?

Scanner inputValue = new Scanner(System.in);

A

It is an instance of the scanner class

115
Q

What is BufferedReader?

A

reads text from an input stream, but it buffers the data to provide more efficiency, can be used in a multi threaded program unlike the Scanner class.

116
Q

Differences between Scanner and BufferedReader:

A

Scanner can check for a specific data type and convert it to a specific data type as our data is obtained from the stream.

BufferedReader is synchronous whereas Scanner is not, so BufferedReader can be shared across multiple threads whereas Scanner cannot

BufferedReader has a bigger default buffer than Scanner 8KB vs 1KB

117
Q

When using the Scanner class to read data from the console, in order to read data which is a double and assign it directly to a double variable you can use the method:

A

nextDouble()

The correct answer is ‘nextDouble()’ which will obtain the next value and convert it to double data type and return it as such.

118
Q

Which is the correct way to specify input from the console using the BufferedReader constructor?

A

BufferedReader(new InputStreamReader(System.in))

because the constructor takes Reader as a parameter and System.in specifies the standard input stream.

119
Q

When using the Scanner class to read data from the console, if the nextInt() method is used but the value in the stream is not an integer but a character, the method will:

A

Throw InputMatchException

If we cannot be sure of the input in the stream we can use the hasX methods first to check what the data type of the next data is.

120
Q

The top, abstract class in Java to represent an input stream of bytes is _____.

A

InputStream.

121
Q

The Scanner class (package java.util.Scanner) is a text scanner that breaks up input into _____.

A

tokens

Explanation
The Scanner class (package java.util.Scanner) is a text scanner that breaks up input into tokens based on a delimiter pattern such as space (default), semi-colon, tab, etc.

122
Q

What is the standard output?

A

The standard output in Java is the PrintStream class accessed through the System.out field. By default, standard output prints to the display, also called the console.

123
Q

Difference between:
System.out.print();
System.out.println();

A

println() will print the next value on the next line

124
Q

\n

A

new line
“\n String”

125
Q

\t

A

tab
“\t String”

126
Q

How to print multiple types of data together?

A

Use a plus sign + to concatenate the data

String name = “Joe”;
int grade = 80;
System.out.println(“Student name: “ + name + “\nStudent grade: “ + grade);

127
Q

The println() method in Java _____.

A

The correct answer is ‘terminates the line’. When the line is terminated, the cursor is moved to the next line.

128
Q

What are the three attributes of the System class?

A

err (PrintStream)
for the standard error output stream

in (InputStream)
for the standard input stream

out (PrintStream)
for the standard output stream.

129
Q

Describe the printf method:

A

System.out.printf

used for formatting the output of numbers and strings. The method takes the format string as the first parameter followed by a list of arguments that are to be formatted. The format string lets you specify the width, right/left justification, decimal precision, special sequences, and other formatting options.

130
Q

What is PrintStream helpful for in standard out?

A

helps format the output

131
Q

The System.out.printf method works exactly the same as the:

A

The correct answer is the format method. The behavior of both methods is identical. The term printf is the name of the standard out method in other languages so Java has it for convenience.

132
Q

To print a floating point number with a precision of 4 decimal places, the correct format string for System.out.printf would be:

A

The correct answer is ‘’%.4f’’ where .4 specifies the precision and f specifies it is a floating point number.

133
Q

To print an integer right justified with a width 15, the correct format string for System.out.printf method would be:

A

The correct answer is ‘‘%15d’’ where 15 specifies the width and since it is positive it is right justified and d specified integer

134
Q

To print a floating point number with a precision of 2 and a width of 6, the correct format string for System.out.printf would be:

A

The correct answer is ‘‘%6.2f’’ where 6 specifies the width, .2 the precision, and f the floating point number.

135
Q

What is a graphical user interface (GUI)?

A

uses visual elements that presents information stored in a computer in an easy to understand manner. These elements make it easy for people to work with and use computer software. A GUI uses windows, icons, and menus to carry out commands, such as opening files, deleting files and moving files. GUI software is much easier to use than command line software since there is no need to type in and memorize individual commands.

136
Q

What does WIMP stand for?

A

window, icon, menu, and pointer

137
Q

Define “usability”:

A

Usability is defined in an official ISO standard as ‘‘the extent to which a product can be used by specified users to achieve specified goals with effectiveness, efficiency, and satisfaction in a specified context of use.’’

138
Q

How would you describe the use of a wizard in the context of computer software applications?

A

A wizard is part of a graphical user interface consisting of a series of pre-defined steps to implement a specific workflow.

139
Q

You are developing a user interface for a software application. How would you proceed with this process?

A

You would perform user analysis, and collect functionality requirements.

140
Q

Attributes of usability in a GUI:

A

Learnability: How easy is it for users to accomplish basic tasks?

Efficiency: How quickly can they perform tasks?

Memorability: How easily can users establish proficiency?

Errors: How many errors do users make?

Satisfaction: How pleasant is it to use the design?

141
Q

What is a container used for?

A

A container is used to store the items the user will see on display, such as a panel or a frame (main window). These are the very core of what holds everything together and are the primary skeletal system of the GUI as a whole.

142
Q

What are components used for in a GUI?

A

Components can be described as the add-ons or extensions to the containers that help to fill in the content and make up the rest of the user interface. Examples can include things such as a textbox, a button, a checkbox, or even a simple label.

143
Q

What are the two classes of GUI in Java?

A

Abstract Window Toolkit (AWT) and Swing

144
Q

What is AWT in Java GUI design?

A

The Abstract Window Toolkit is mostly the set of tools or programs that give a programmer the ability to create the components of the GUI itself, such as the buttons or scroll bars. They are known as the heavyweight components and are explicitly bound by the platform or operating system they are being used on.

145
Q

What is Swing in Java GUI design?

A

Swing does the same thing as AWT, but as extensions to the AWT itself. These are known as the lightweight components and are entirely independent of the platform or operating system, which give them much more freedom and flexibility than the AWT.

146
Q

When it comes to Java, a(n) _____ is used to fill up the rest of the container with content and can be called its extension.

A

Component

147
Q

In order to create a polygon, what do you need to do first?

A

Polygon has its own class and you need to create a new instance of the Polygon class, and then set the points for each part of the shape.

148
Q

How would you draw a rectangle that is a solid color?

A

g2.fillRect(10, 20, 100, 100);

149
Q

If you were to create a rounded rectangle, which option correctly creates the shape?

A

Rounded rectangles include starting and ending x, y coordinates as well as the angle of the arc/sweep on the corners.

150
Q

Which class allows you to set the thickness of a line?

A

g2.setStroke(new BasicStroke(5));

151
Q

In the case of Swing, what is the container used for an application?

A

JFrame

152
Q

Explain the difference between methods and constructors in JFrame:

A

Methods are functions that impact the JFrame such as size and visibility, constructors are run when the instance is created

153
Q

You have created a new JFrame (jf) and added several other Swing elements. Which code statement correctly ensures that you will see your new GUI application on screen?

A

jf.setVisible(true);

154
Q

Which of the following correctly creates a new JFrame without a title?

A

JFrame jf = new JFrame();

155
Q

How can the text in a label component be changed?

A

Programatically at run time. Values of label components can only be changed programatically at run time.

156
Q

How to create a JLabel with text?

A

JLabel jl = new JLabel(“New Label”);

157
Q

When creating a JLabel that has an icon instead of text, what do you need to create first?

A

An instance of ImageIcon

158
Q

In order to add a JLabel to a GUI application, which component is needed first?

A

JFrame

159
Q

How would you change the text of a JLabel (lblUser) to “Enter User Name?”

A

lblUser.setText(“Enter User Name”);

160
Q

What is it called when components are not manually laid out?

A

absolute positioning

161
Q

Which containers form the base of the GUI application structure?

A

JPanel and content panes

162
Q

Describe the Box Layout:

A

javax.swing.BoxLayout

This is a type of layout manager that allows multiple components to be laid out either vertically or horizontally. It does not allow any form of wrapping, hence, the vertical arrangement of components remains vertically arranged even when the frame is resized.

163
Q

Describe the Group Layout:

A

javax.swing.GroupLayout

It works with vertical and horizontal layouts differently and each dimension is defined independently. And as a consequence, each component needs to be defined twice within the layout.

164
Q

Describe the Scroll Pane Layout:

A

javax.swing.ScrollPaneLayout

This class is implemented as the layout manager used by JScrollPane and it is used by several components such as two scrollbars, a row header, a column header, a viewport and four corner components.

165
Q

Describe the Spring Layout:

A

javax.swing.SpringLayout

This is a flexible layout manager that lets you specify precise relationships between the edges of components under its control. It designed for use by GUI builders.

166
Q

The layout manager object is an implementation of which container?

A

layout manager interface

167
Q

This method determines where an element will be displayed on the screen and its size:

A

setBounds

textBox.setBounds(x, y, height, width)

This is the syntax for setting the original starting position (x,y) and the height and width of a JTextField or JButton element.

168
Q

As you are building a GUI application, you want to add a text entry field, but default it with some text. Which line of code achieves this task?

A

JTextField text1 = new JTextField(“[Enter User Name]”);

This command sets the text within the text box. This is not required but sometimes useful when building applications.

169
Q

Tool tips are:

A

Methods

Tooltip, unlike JTextField and JButton, is not a class in Java. It is, rather, a functionality that is available to GUI components and is implemented as a method.

170
Q
A
171
Q

What JTextFields method is used for managing action events?

A

void addActionListener(ActionListener l)

is responsible for adding the specified action listener to receive action events from the text field.

172
Q

Which of the following is required before adding text fields, labels, buttons, or tool tips?

A

JFrame

The very first task to perform in Java GUI programming is to create an object using the JFrame class.

173
Q

What is an event in Java using Swing?

A

a user’s interaction with a component.

174
Q

What is the event source?

A

The object that is triggered in the event.

175
Q

What is an Event Listener?

A

Code that listens for changes, additions, user interaction, etc. When one of these actions is performed, it then does something based on that event.

176
Q

What is an event object?

A

the event itself

177
Q

ActionListener

A

Responds to actions (like a button click or text field change)

178
Q

ItemListener

A

Listens for individual item changes (like a checkbox)

179
Q

KeyListener

A

Listens for keyboard interaction

180
Q

MouseListener

A

Listens for mouse events (like double-click, right-click, etc.)

181
Q

MouseMotionListener

A

Listens for mouse motion

182
Q

WindowListener

A

Acts on events in the window

183
Q

ContainerListener

A

Listens for container (like JFrame or JPanel) events

184
Q

ComponentListener

A

Listens for changes to components (like a label being moved, hidden, shown, or resized in the program)

185
Q

AdjustmentListener

A

Listens for adjustments (like a scroll bar being engaged)

186
Q

How to code a listener to a button?

A

button.addActionListener(new ActionListener()) { }

187
Q

If you wanted to know if a user selected ‘‘Oranges’’ from a list of check boxes, which event listener is most appropriate?

A

The ItemListener helps you identify if a given item (such as a check box) has triggered an event.

188
Q

How to use the ActionListener interface?

A

addActionListener()

189
Q

How many options can the user select out of a JCheckBox?

A

As many items as they want

190
Q

What GUI class serves as a foundation on which to add elements?

A

JFrame

191
Q

Consider the following Java code. What is the name of the constructor for the class being instantiated?

JFrame thisFrame = new JFrame(‘Hi There’);

A

JFrame()

Constructors share the name of the class. Therefore the constructor for JFrame is JFrame(), and it accepts a parameter; in this case we are sending text.

192
Q

The Graphics2D class is an extension of what other Java class?

A

Graphics

193
Q

How to create a rectangle using the Graphics2D instance named g3?

A

g3.draw(new Rectangle2D.Double(20, 100, 100, 50));

194
Q

What is the Java classes is used to create and render 2-dimensional graphics?

A

Graphics2D class is used to render 2-dimensional shapes, text and images, as well as provide color management, and text layout in Java GUI applications.

195
Q

Which place is the best to look for all the methods available in the Graphics2D class?

A

Javadocs