Java (Ch 1 - Ch 7)

Description

Computer Science Quiz on Java (Ch 1 - Ch 7), created by Heather Williams on 23/06/2018.
Heather Williams
Quiz by Heather Williams, updated more than 1 year ago
Heather Williams
Created by Heather Williams over 6 years ago
227
0

Resource summary

Question 1

Question
Which of the following statements opens a file named MyFile.txt and allows you to append data to its existing contents?
Answer
  • PrintWriter outfile = new PrintWriter("MyFile.txt", true);
  • PrintWriter outfile = new PrintWriter(true, "MyFile.txt");
  • FileWriter fwriter = new FileWriter("MyFile.txt"); PrintWriter outFile = new PrintWriter(fwriter);
  • FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);

Question 2

Question
When an object is created, the attributes associated with the object are called
Answer
  • instance fields
  • fixed attributes
  • instance methods
  • class instances

Question 3

Question
What will be the value of x after the following code is executed?
Answer
  • 110
  • 90
  • 130
  • 210

Question 4

Question
Which of the following strings could be passed to the DecimalFormat constructor to display 12.78 as 12.8%?
Answer
  • "###.##%"
  • "#0.00%"
  • "##0.0%"
  • "000.0%"

Question 5

Question
Given the following statement, which statement will write the string "Calvin" to the file DiskFile.txt?
Answer
  • PrintWriter.println("Calvin");
  • diskOut.println("Calvin");
  • System.out.println(diskOut, "Calvin");
  • DiskFile.println("Calvin");

Question 6

Question
What would be the value of discountRate after the following statements are executed?
Answer
  • 0.03
  • 0.0
  • 0.01
  • 0.05

Question 7

Question
The ________ is ideal in situations where the exact number of iterations is known.
Answer
  • posttest loop
  • while loop
  • do-while loop
  • for loop

Question 8

Question
What will be the result of executing the following code?
Answer
  • The variable x will contain the values 0 through 5.
  • An array of 6 values, all initialized to 0 and referenced by the variable x, will be created.
  • A compiler error will occur.
  • An array of 6 values, ranging from 0 through 5 and referenced by the variable x, will be created.

Question 9

Question
What will be displayed when the following code is executed?
Answer
  • 45678.259
  • 45,678.26
  • 0,045,678.26
  • 45,678.3

Question 10

Question
What will be the value of bonus after the following code is executed?
Answer
  • 1000
  • 500
  • 1250
  • 750

Question 11

Question
In an if-else statement, if the boolean expression is false
Answer
  • no statements or blocks are executed.
  • the statement or block following the else is executed.
  • the first statement or block is executed.
  • all statements or blocks are executed.

Question 12

Question
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?
Answer
  • while (inputFile.hasNext())
  • while (!inputFile.EOF)
  • while (inputFile.nextLine == " ")
  • while (inputFile != null)

Question 13

Question
What would be the value of discountRate after the following statements are executed?
Answer
  • 0.04
  • 0.0
  • 0.03
  • 0.05

Question 14

Question
A ________ is a boolean variable that signals when some condition exists in the program.
Answer
  • flag
  • block
  • case
  • sentinel

Question 15

Question
You should not define a class field that is dependent upon the values of other class fields
Answer
  • in order to avoid having stale data.
  • because it should be defined in another class.
  • in order to keep it current.
  • because it is redundant.

Question 16

Question
Variables are classified according to their
Answer
  • values
  • data type
  • names
  • location in memory.

Question 17

Question
A class that is defined inside of another class is called a(n)
Answer
  • inner class
  • helper class
  • nested class
  • enumerated class

Question 18

Question
Because Java byte code is the same on all computers, compiled Java programs
Answer
  • must be re-compiled for each different machine it is run on.
  • cannot run on computers with different operating systems.
  • are highly portable.
  • are non-existent.

Question 19

Question
Java requires that the boolean expression being tested by an if statement be enclosed in
Answer
  • a set of double quotes.
  • a set of brackets.
  • a set of braces.
  • a set of parentheses.

Question 20

Question
The scope of a local variable is
Answer
  • the entire class.
  • inside the parentheses of a method header.
  • the method in which they are defined.
  • inside the class, but not inside any method.

Question 21

Question
What will be returned from the method, if the following is the method header?
Answer
  • the address of an object of the Rectangle class
  • the values stored in the data members of the Rectangle object
  • a null value
  • an object that is contained in the class Rectangle

Question 22

Question
How many bits are in a byte?
Answer
  • 4
  • 8
  • 16
  • 32

Question 23

Question
What will be the value of x after the following code is executed?
Answer
  • 110
  • 90
  • 10
  • 100

Question 24

Question
If you do not provide initialization values for a class's numeric fields, they will
Answer
  • cause a runtime error.
  • contain an unknown value.
  • cause a compiler error.
  • be automatically initialized with 0.

Question 25

Question
________ operators are used to determine whether a specific relationship exists between two values.
Answer
  • Relational
  • Arithmetic
  • Logical
  • Assignment

Question 26

Question
Using a counter-controlled while loop, write Java code to compute the sum of the integers from 1 to 100, inclusively, and output that result to the console. You will declare the integer variable num to count the number of executions (in the counter-controlled loop) and initialize it to 1. You will also declare the integer variable sum to store your computation result and initialize it to 0. Be sure to print your result to the console.
Answer
  • True
  • False

Question 27

Question
How many times will the following for loop be executed?
Answer
  • 11
  • 12
  • 10
  • 0

Question 28

Question
This type of loop will always be executed at least once.
Answer
  • pretest
  • posttest
  • infinite
  • conditional

Question 29

Question
Before entering a loop to compute a running total, the program should first
Answer
  • know exactly how many values there are to total.
  • set all variables to zero.
  • read all the values into main memory.
  • set the accumulator variable to an initial value, usually zero.

Question 30

Question
Class objects normally have ________ that perform useful operations on their data, but primitive variables do not.
Answer
  • instances
  • relationships
  • methods
  • fields

Question 31

Question
After the header, the body of the method appears inside a set of
Answer
  • brackets, [].
  • braces, {}.
  • parentheses, ().
  • double quotes, "" .

Question 32

Question
A for loop normally performs which of these steps?
Answer
  • update the control variable during each iteration
  • test the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value
  • initialize a control variable to a starting value
  • All of these

Question 33

Question
In UML diagrams, what symbol indicates that a member is public?
Answer
  • *
  • +
  • #
  • -

Question 34

Question
How many times will the following do-while loop be executed?
Answer
  • 3
  • 5
  • 1
  • 4

Question 35

Question
What would be the value of discountRate after the following statements are executed?
Answer
  • 0.04
  • 0.0
  • 0.06
  • 0.08

Question 36

Question
A method (NEED TO FIND ANSWER)
Answer
  • must have at least two parameter variables. (NOT THIS ONE)
  • may have zero or more parameter variables.
  • may have only one parameter variables.
  • never has parameter variables.

Question 37

Question
What is the result of the following expression?
Answer
  • -50
  • 25
  • 5
  • -5

Question 38

Question
What will be the result of the following code?
Answer
  • The value variable will contain the lowest value in the numbers array.
  • The value variable will contain the average of all the values in the numbers array.
  • The value variable will contain the sum of all the values in the numbers array.
  • The value variable will contain the highest value in the numbers array.

Question 39

Question
A loop that executes as long as a particular condition exists is called a(n) ________ loop.
Answer
  • count-controlled
  • infinite
  • relational
  • conditional

Question 40

Question
UML diagrams do not contain
Answer
  • object names
  • class names
  • methods
  • fields

Question 41

Question
To print "Hello, world" on the monitor, use the following Java statement:
Answer
  • SystemOutPrintln('Hello, world');
  • system.out.println{Hello, world};
  • System.out.println("Hello, world");
  • System Print = "Hello, world";

Question 42

Question
If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal?
Answer
  • str1 == str2
  • str1 + str2
  • str1 && str2
  • str1.equals(str2)

Question 43

Question
What will be displayed after the following code has been executed?
Answer
  • 10
  • 100
  • The code contains an error and will not compile.
  • 120

Question 44

Question
The key word new
Answer
  • creates a new class.
  • creates an object in memory.
  • creates a new Java byte code file.
  • creates a new variable in memory.

Question 45

Question
Which of the following expressions could be used to perform a case-insensitive comparison of two String objects named str1 and str2?
Answer
  • str1 || str2
  • str1.equalsInsensitive(str2)
  • str1 != str2
  • str1.equalsIgnoreCase(str2)

Question 46

Question
How many times will the following do-while loop be executed?
Answer
  • 1
  • 4
  • 5
  • 0

Question 47

Question
What will be the value of x after the following code is executed?
Answer
  • 30
  • 40
  • 50
  • 25

Question 48

Question
A UML diagram uses the ________ symbol to indicate that a member is private.
Answer
  • -
  • #
  • +
  • *

Question 49

Question
What will be the value of x after the following code is executed?
Answer
  • 135
  • 60
  • 75
  • 15

Question 50

Question
Which of the following is not a rule that must be followed when naming identifiers?
Answer
  • Uppercase and lowercase characters are distinct.
  • The first character must be one of the letters a-z, A-Z, an underscore or a dollar sign.
  • After the first character, you may use the letters a-z, A-Z, an underscore, a dollar sign, or digits 0-9.
  • Identifiers can contain spaces.

Question 51

Question
A computer program is
Answer
  • a set of instructions that enable the computer to solve a problem or perform a task.
  • pseudocode.
  • a flow chart.
  • main memory

Question 52

Question
In all but rare cases, loops must contain within themselves
Answer
  • arithmetic operators.
  • if statements.
  • nested loops.
  • a way to terminate.

Question 53

Question
A method's signature consists of
Answer
  • the return type, the method name, and the parameter list.
  • the return type and the method name.
  • the method name and the parameter list.
  • the size of the method in memory.

Question 54

Question
What does the following UML diagram entry mean?
Answer
  • a private method with no parameters that returns a double data type
  • a public method with a parameter of data type double that does not return a value
  • a private field called Height that is a double data type
  • a public field called Height that is a double data type

Question 55

Question
Using a for loop, write Java code that prints out all multiples of 5 from 0 to 100, inclusively, to the console.
Answer
  • True
  • False

Question 56

Question
A loop that repeats a specific number of times is known as a(n) ________ loop.
Answer
  • pretest
  • count-controlled
  • infinite
  • conditional

Question 57

Question
What will be displayed when the following code is executed?
Answer
  • 45,678.3
  • 45,678.259
  • 45,678.26
  • 45678.259

Question 58

Question
What will be the values of ans, x, and y after the following statements are executed?
Answer
  • ans = 45, x = 50, y = 50
  • ans = 60, x = 0, y = 50
  • ans = 45, x = 50, y = 0
  • ans = 60, x = 50, y =100

Question 59

Question
What would be the value of x after the following statements were executed?
Answer
  • 5
  • 20
  • 30
  • 25

Question 60

Question
When working with the PrintWriter class, which of the following import statements should you have near the top of your program?
Answer
  • import javax.swing.*;
  • import java.file.*;
  • import javac.io.*;
  • import java.io.*;

Question 61

Question
The boolean expression in an if statement must evaluate to
Answer
  • positive or negative.
  • degrees or radians.
  • left or right.
  • true or false.

Question 62

Question
This is a named storage location in the computer's memory.
Answer
  • Constant
  • Variable
  • Literal
  • Operator

Question 63

Question
Methods that operate on an object's fields are called
Answer
  • instance methods
  • instance variables
  • private methods
  • public methods

Question 64

Question
The variable used to keep the running total is called a(n)
Answer
  • sentinel
  • summation
  • accumulator
  • integer

Question 65

Question
If object1 and object2 are objects of the same class, to make object2 a copy of object1
Answer
  • use an assignment statement to make object2 a copy of object1.
  • use the copy method that is a part of the Java API.
  • write a method for the class that will make a field by field copy of object1 data members into object2 data members.
  • use the default constructor to create object2 with object1 data members.

Question 66

Question
If you wish to use the System.out.printf method to print a string argument, use the ________ format specifier.
Answer
  • %f
  • %s
  • %d
  • %b

Question 67

Question
A static field is created by placing the key word static
Answer
  • in brackets, before the field's data type.
  • after the field name.
  • after the access specifier and field's data type.
  • after the access specifier and before the field's data type.

Question 68

Question
Character literals are enclosed in ________, and string literals are enclosed in ________.
Answer
  • single quotes, double quotes
  • double quotes, double quotes
  • single quotes, single quotes
  • double quotes, single quotes

Question 69

Question
What will be the value of x after the following code is executed?
Answer
  • 14
  • 16
  • 15
  • 0

Question 70

Question
Which of the following expressions determines whether the char variable chrA is not equal to the letter 'A'?
Answer
  • chrA || 'A'
  • chrA == 'A'
  • chrA.notEquals('A')
  • chrA != 'A'

Question 71

Question
A block of code is enclosed in a set of
Answer
  • brackets
  • braces
  • double quotes
  • parentheses

Question 72

Question
A class's responsibilities include
Answer
  • both the things a class is responsible for knowing and the things a class is responsible for doing
  • neither the things a class is responsible for knowing nor the things a class is responsible for doing
  • the things a class is responsible for knowing.
  • the things a class is responsible for doing.

Question 73

Question
What is syntax? (NEED RIGHT ANSWER)
Answer
  • Words that have a special meaning in the programming language.
  • Words or names defined by the programmer. (NOT ANSWER)
  • Rules that must be followed when writing a program.
  • Symbols or words that perform operations.

Question 74

Question
Which of the following statements opens a file named MyFile.txt and allows you to read data from it?
Answer
  • File file = new File("MyFile.txt");
  • File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
  • Scanner inputFile = new Scanner("MyFile.txt");
  • PrintWriter inputFile = new PrintWriter("MyFile.txt");

Question 75

Question
What is the value of ans after the following code has been executed?
Answer
  • 30
  • 50
  • 80
  • The code contains an error and will not compile.

Question 76

Question
The ________ statement is used to create a decision structure, which allows a program to have more than one path of execution.
Answer
  • null
  • block
  • flag
  • if

Question 77

Question
Rewrite the following if-else if statement as a switch statement:
Answer
  • True
  • False

Question 78

Question
What will be displayed when the following code is executed? (NEED RIGHT ANSWER)
Answer
  • 30
  • The code contains an error and will not compile. (NOT ANSWER)
  • 20
  • 40

Question 79

Question
What is the result of the following expression?
Answer
  • 105
  • 8
  • 12
  • 7

Question 80

Question
A search algorithm
Answer
  • arranges elements in ascending order.
  • is rarely used with arrays.
  • arranges elements in descending order.
  • is used to locate a specific item in a larger collection of data.

Question 81

Question
Overloading is
Answer
  • having two or more methods with the same name, but different signatures.
  • having two or more methods with the same signature.
  • writing a method that does too much processing.
  • writing a program that is too large to fit in memory. (NOT RIGHT)

Question 82

Question
A constructor
Answer
  • always has an access specifier of private.
  • always accepts two arguments.
  • has return type of void.
  • has the same name as the class.

Question 83

Question
Data hiding, which means that critical data stored inside the object is protected from code outside the object, is accomplished in Java by
Answer
  • using the public access specifier on the class methods.
  • using the private access specifier on the class methods.
  • using the private access specifier on the class fields.
  • using the private access specifier on the class definition.

Question 84

Question
Which of the following expressions will determine whether x is less than or equal to y?
Answer
  • x =< y
  • x <= y
  • x >= y
  • x => y

Question 85

Question
When saving a Java source file, save it with an extension of
Answer
  • .class.
  • .java.
  • .javac.
  • .src.

Question 86

Question
Another term for an object of a class is a(n)
Answer
  • instance
  • method
  • member
  • access specifier

Question 87

Question
Which of the following statements determines whether temp is within the range of 0 through 100?
Answer
  • if (temp >= 0 && temp <= 100)
  • if (temp > 0 || temp < 100)
  • if (temp >= 0 || temp <= 100)
  • if (temp > 0 && temp < 100)

Question 88

Question
Using a switch statement, write Java code to output the season of the year to the console based on the value of an integer variable called month according to the following:
Answer
  • True
  • False

Question 89

Question
What will be the result of the following code? (NEED ANSWER)
Answer
  • All the values in the array will be initialized to 10.0.
  • A runtime error will occur. (NOT RIGHT)
  • All the values in the array, except the first, will be set to 10.0.
  • The code contains a syntax error and will not compile.

Question 90

Question
A constructor is a method that
Answer
  • removes the object from memory.
  • performs initialization or setup operations.
  • never receives any arguments.
  • returns an object of the class.

Question 91

Question
A group of related classes is called a(n)
Answer
  • archive
  • package
  • collection
  • attachment

Question 92

Question
Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file?
Answer
  • int number = inputFile.next();
  • int number = inputFile.integer();
  • int number = inputFile.readInt();
  • int number = inputFile.nextInt();

Question 93

Question
What does the following code do?
Answer
  • It establishes a connection with a file named filename.
  • It allows the user to enter the name of the file that data will to be written to.
  • Nothing. The code contains a syntax error.
  • It writes to a file named filename.

Question 94

Question
The ________ loop is ideal in situations where you always want the loop to iterate at least once.
Answer
  • for
  • do-while
  • pretest
  • while

Question 95

Question
A ________ is a value that signals when the end of a list of values has been reached.
Answer
  • sentinel
  • token
  • delimiter
  • terminal

Question 96

Question
________ is the term for the relationship created by object aggregation.
Answer
  • Inner Class
  • One-to-many
  • "Is a"
  • "Has a"

Question 97

Question
Which Scanner class method reads a String?
Answer
  • nextLine
  • charAt
  • nextString
  • getLine

Question 98

Question
Enclosing a group of statements inside a set of braces creates
Answer
  • a block of statements.
  • a relational operator.
  • an if-else statement.
  • an expression.

Question 99

Question
This statement tells the compiler where to find the JOptionPane class and makes it available to your program.
Answer
  • import javax.JOptionPane;
  • import javax.swing.JOptionPane;
  • import Java.Swing.JOptionPane;
  • import JOptionPane;

Question 100

Question
Which of the following is not a valid Java comment?
Answer
  • /** Comment 1 */
  • // Comment 3
  • */ Comment 2 /*
  • /* Comment 4 */
Show full summary Hide full summary

Similar

Computing Hardware - CPU and Memory
ollietablet123
SFDC App Builder 2
Parker Webb-Mitchell
Data Types
Jacob Sedore
Intake7 BIM L1
Stanley Chia
Software Processes
Nurul Aiman Abdu
Design Patterns
Erica Solum
CCNA Answers – CCNA Exam
Abdul Demir
Abstraction
Shannon Anderson-Rush
Spyware
Sam2
HTTPS explained with Carrier Pigeons
Shannon Anderson-Rush
Data Analytics
anelvr