Quiz Questions For Chapter 1
Quiz Questions For Chapter 1
Quiz Questions For Chapter 1
For Chapter 1
Question 1: One of the components of a computer is its CPU. What is a CPU and what role
does it play in a computer?
Question 4: Explain the difference between high-level languages and machine language.
Question 5: If you have the source code for a Java program, and you want to run that
program, you will need both a compiler and an interpreter. What does the Java compiler do,
and what does the Java interpreter do?
Question 8: What is a variable? (There are four different ideas associated with variables in
Java. Try to mention all four aspects in your answer. Hint: One of the aspects is the variable's
name.)
Question 10: What is the "Internet"? Give some examples of how it is used. (What kind of
services does it provide?)
Sample Quiz Answers
For Chapter 1
Question 1: One of the components of a computer is its CPU. What is a CPU and what role
does it play in a computer?
Answer: The CPU, or Central Processing Unit, is the active part of the computer. Its function
is to execute programs that are coded in machine language and stored in the main memory
(RAM) of the computer. It does this by repeating the fetch-and-execute cycle over and over;
that is, it repeatedly fetches a machine language instruction from memory and executes it.
Answer: An asynchronous event is one that occurs at an unpredictable time outside the
control of the program that the CPU is running. It is not "synchronized" with the program. An
example would be when the user presses a key on the keyboard or clicks the mouse button.
(These events generate "interrupts" that cause the CPU to interrupt what it is doing and to take
some action to handle the asynchronous event. After handling the event, the CPU returns to
what it was doing before it was interrupted.)
Answer: Compilers and interpreters have similar functions: They take a program written in
some programming language and translate it into machine language. A compiler does the
translation all at once. It produces a complete machine language program that can then be
executed. An interpreter, on the other hand, just translates one instruction at a time, and then
executes that instruction immediately. (Java uses a compiler to translate java programs into
Java Bytecode, which is a machine language for the imaginary Java Virtual Machine. Java
Bytecode programs are then executed by an interpreter.)
Question 4: Explain the difference between high-level languages and machine language.
Answer: Programs written in the machine language of a given type of computer can be
directly executed by the CPU of that type of computer. High-level language programs must be
translated into machine language before they can be executed. (Machine language instructions
are encoded as binary numbers that are meant to be used by a machine, not read or written by
people. High-level languages use a syntax that is closer to human language.)
Question 5: If you have the source code for a Java program, and you want to run that
program, you will need both a compiler and an interpreter. What does the Java compiler do,
and what does the Java interpreter do?
Answer: The Java compiler translates Java programs into a language called Java bytecode.
Although bytecode is similar to machine language, it is not the machine language of any
actual computer. A Java interpreter is used to run the compiled Java bytecode program. (Each
type of computer needs its own Java bytecode interpreter, but all these interpreters interpret
the same bytecode language.)
Answer: A subroutine is a set of instructions for performing some task that have been
grouped together and given a name. Later, when that task needs to be performed, it is only
necessary to call the subroutine by giving its name, rather than repeating the whole sequence
of instructions.
Answer: An object consists of some data together with a set of subroutines that manipulate
that data. (An object is a kind of "module," or self-contained entity that communicates with
the rest of the world through a well-defined interface. An object should represent some
coherent concept or real-world object.)
Question 8: What is a variable? (There are four different ideas associated with variables in
Java. Try to mention all four aspects in your answer. Hint: One of the aspects is the variable's
name.)
Answer: A variable is a memory location that has been given a name so that it can easily be
referred to in a program. The variable holds a value, which must be of some specified type.
The value can be changed during the course of the execution of the program.
Answer: A Java program can be compiled once into a Java Bytecode program. The compiled
program can then be run on any computer that has an interpreter for the Java virtual machine.
Other languages have to be re-compiled for each platform on which they are going to run. The
point about Java is that it can be executed on many different types of computers without being
recompiled.
Question 10: What is the "Internet"? Give some examples of how it is used. (What kind of
services does it provide?)
Answer: The Internet is a network connecting millions of computers around the world.
Computers connected to the Internet can communicate with each other. The Internet can be
used for Telnet (which lets a user of one computer log onto another computer remotely), FTP
(which is used to copy files between computers), and the World Wide Web (which lets a user
view "pages" of information published on computers around the world).
Quiz Questions
For Chapter 2
Question 1: Briefly explain what is meant by the syntax and the semantics of a programming
language. Give an example to illustrate the difference between a syntax error and a semantics
error.
Question 2: What does the computer do when it executes a variable declaration statement.
Give an example.
Question 4: One of the primitive types in Java is boolean. What is the boolean type? Where
are boolean values used? What are its possible values?
a) ++
b) &&
c) !=
Question 6: Explain what is meant by an assignment statement, and give an example. What
are assignment statements used for?
Question 9: In Java, classes have two fundamentally different purposes. What are they?
Question 10: What is the difference between the statement "x = TextIO.getDouble();"
and the statement "x = TextIO.getlnDouble();"
Question 1: Explain briefly what is meant by "pseudocode" and how is it useful in the
development of algorithms.
Question 2: What is a block statement? How are block statements used in Java programs.
Question 3: What is the main difference between a while loop and a do..while loop?
Question 6: Write a for loop that will print out all the multiples of 3 from 3 to 36, that is: 3 6
9 12 15 18 21 24 27 30 33 36.
Question 7: Fill in the following main() routine so that it will ask the user to enter an integer,
read the user's response, and tell the user whether the number entered is even or odd. (You
can use TextIO.getInt() to read the integer. Recall that an integer n is even if n % 2 == 0.)
Question 8: Show the exact output that would be produced by the following main() routine:
Question 9: Show the exact output produced by the following main() routine:
String name;
int i;
boolean startWord;
Question 1: A "black box" has an interface and an implementation. Explain what is meant by
the terms interface and implementation.
Question 3: Briefly explain how subroutines can be a useful tool in the top-down design of
programs.
Question 4: Discuss the concept of parameters. What are parameters for? What is the
difference between formal parameters and actual parameters?
Question 5: Give two different reasons for using named constants (declared with the final
modifier).
Question 7: Write a subroutine named "stars" that will output a line of stars to standard
output. (A star is the character "*".) The number of stars should be given as a parameter to the
subroutine. Use a for loop. For example, the command "stars(20)" would output
********************
Question 8: Write a main() routine that uses the subroutine that you wrote for Question 7 to
output 10 lines of stars with 1 star in the first line, 2 stars in the second line, and so on, as
shown below.
*
**
***
****
*****
******
*******
********
*********
**********
Question 9: Write a function named countChars that has a String and a char as parameters.
The function should count the number of times the character occurs in the string, and it should
return the result as the value of the function.
Question 10: Write a subroutine with three parameters of type int. The subroutine should
determine which of its parameters is smallest. The value of the smallest parameter should be
returned as the value of the subroutine.
Quiz Questions
For Chapter 5
Question 1: Object-oriented programming uses classes and objects. What are classes and
what are objects? What is the relationship between classes and objects?
Question 2: Explain carefully what null means in Java, and why this special value is
necessary.
Question 4: Suppose that Kumquat is the name of a class and that fruit is a variable of type
Kumquat. What is the meaning of the statement "fruit = new Kumquat();"? That is, what
does the computer do when it executes this statement? (Try to give a complete answer. The
computer does several things.)
Question 5: What is meant by the terms instance variable and instance method?
Question 8: Java uses "garbage collection" for memory management. Explain what is meant
here by garbage collection. What is the alternative to garbage collection?
Question 9: For this problem, you should write a very simple but complete class. The class
represents a counter that counts 0, 1, 2, 3, 4,.... The name of the class should be Counter. It
has one private instance variable representing the value of the counter. It has two instance
methods: increment() adds one to the counter value, and getValue() returns the current
counter value. Write a complete definition for the class, Counter.
Question 10: This problem uses the Counter class from Question 9. The following program
segment is meant to simulate tossing a coin 100 times. It should use two Counter objects,
headCount and tailCount, to count the number of heads and the number of tails. Fill in the
blanks so that it will do so.
else
Question 1: Programs written for a graphical user interface have to deal with "events."
Explain what is meant by the term event. Give at least two different examples of events, and
discuss how a program might respond to those events.
Question 5: Draw the picture that will be produced by the following paint() method:
Question 6: Suppose you would like an applet that displays a green square inside a red circle,
as illustrated. Write a paint() method that will draw the image.
Question 7: Suppose that you are writing an applet, and you want the applet to respond in
some way when the user clicks the mouse on the applet. What are the four things you need to
remember to put into the source code of your applet?
Question 8: Java has a standard class called MouseEvent. What is the purpose of this class?
What does an object of type MouseEvent do?
Question 9: Explain what is meant by input focus. How is the input focus managed in a Java
GUI program?
Question 10: Java has a standard class called JPanel. Discuss two ways in which JPanels can
be used.
Question 2: An off-screen image can be used to do double buffering. Explain this. (What are
off-screen images? How are they used? Why are they important? What does this have to do
with animation?)
Question 3: One of the main classes in Swing is the JComponent class. What is meant by a
component? What are some examples?
Question 5: What does it mean to use a null layout manager, and why would you want to do
so?
Question 9: Menus can contain sub-menus. What does this mean, and how are sub-menus
handled in Java?
Question 1: What does the computer do when it executes the following statement? Try to
give as complete an answer as possible.
Question 4: What is meant by a dynamic array? What is the advantage of a dynamic array
over a regular array?
Question 5: What is the purpose of the following subroutine? What is the meaning of the
value that it returns, in terms of the value of its parameter?
Question 6: Show the exact output produced by the following code segment.
Question 7: Write a complete subroutine that finds the largest value in an array of ints. The
subroutine should have one parameter, which is an array of type int[]. The largest number in
the array should be returned as the value of the subroutine.
Question 8: Suppose that temperature measurements were made on each day of 1999 in each
of 100 cities. The measurements have been stored in an array
class Employee {
String lastName;
String firstName;
double hourlyWage;
int yearsWithCompany;
}
Write a code segment that will output the first name, last name, and hourly wage of each
employee who has been with the company for 20 years or more.
Question 10: Suppose that A has been declared and initialized with the statement
And suppose that A has already been filled with 20 values. Write a program segment that will
find the average of all the non-zero numbers in the array. (The average is the sum of the
numbers, divided by the number of numbers. Note that you will have to count the number of
non-zero entries in the array.) Declare any variables that you use.
Question 2: Why do programming languages require that variables be declared before they
are used? What does this have to do with correctness and robustness?
Question 5: Explain how preconditions can be used as an aid in writing correct programs.
Question 6: Java has a predefined class called Throwable. What does this class represent?
Why does it exist?
Question 7: Write a subroutine that prints out a 3N+1 sequence starting from a given integer,
N. The starting value should be a parameter to the subroutine. If the parameter is less than or
equal to zero, throw an IllegalArgumentException. If the number in the sequence becomes
too large to be represented as a value of type int, throw an ArithmeticException.
Question 8: Some classes of exceptions require mandatory exception handling. Explain what
this means.
Write a try...catch statement that calls this subroutine and prints an error message if an
IOException occurs.
Question 10: Why should a subroutine throw an exception when it encounters an error? Why
not just terminate the program?
Question 1: In Java, input/output is done using streams. Streams are an abstraction. Explain
what this means and why it is important.
Question 2: Java has two types of streams: character streams and byte streams. Why? What is
the difference between the two types of streams?
Why would you need a statement that involves two different stream classes, PrintWriter
and FileWriter?
Question 5: The package java.io includes a class named URL. What does an object of type
URL represent, and how is it used?
Question 6: Explain what is meant by the client / server model of network communication.
Question 9: Network server programs are often multithreaded. Explain what this means and
why it is true.
Question 10: Write a complete program that will display the first ten lines from a text file.
The lines should be written to standard output, System.out. The file name is given as the
command-line argument args[0]. You can assume that the file contains at least ten lines.
Don't bother to make the program robust.
Show the output that would be produced by the subroutine calls printStuff(0),
printStuff(1), printStuff(2), and printStuff(3).
Question 3: Suppose that a linked list is formed from objects that belong to the class
class ListNode {
int item; // An item in the list.
ListNode next; // Pointer to next item in the list.
}
Write a subroutine that will find the sum of all the ints in a linked list. The subroutine should
have a parameter of type ListNode and should return a value of type int.
Question 6: What is an activation record? What role does a stack of activation records play
in a computer?
Question 7: Suppose that a binary tree is formed from objects belonging to the class
class TreeNode {
int item; // One item in the tree.
TreeNode left; // Pointer to the left subtree.
TreeNode right; // Pointer to the right subtree.
}
Write a recursive subroutine that will find the sum of all the nodes in the tree. Your
subroutine should have a parameter of type TreeNode, and it should return a value of type
int.
Question 8: What is a postorder traversal of a binary tree?
where a <word> can be any sequence of letters. Give five different <multilist>'s that can
be generated by this rule. (This rule, by the way, is almost the entire syntax of the
programming language LISP! LISP is known for its simple syntax and its elegant and
powerful semantics.)
Question 2: Java does not support generic programming with the primitive types. Why not?
What is it about generic programming in Java that prevents it from working with primitive
types such as int and double.
Question 3: What is an iterator and why are iterators necessary for generic programming?
Question 4: Suppose that integers is a variable of type Collection and that every object in
the collection belongs to the wrapper class Integer. Write a code segment that will compute
the sum of all the integer values in the collection.
Question 5: Interfaces such as List, Set, and Map define abstract data types. Explain what
this means.
Question 6: What is the fundamental property that distinguishes Sets from other types of
Collections?
Question 9: Modify the following Date class so that it implements the Comparable interface.
The ordering on objects of type Date should be the natural, chronological ordering.
class Date {
int month; // Month number in range 1 to 12.
int day; // Day number in range 1 to 31.
int year; // Year number.
Date(int m, int d, int y) { // Convenience constructor.
month = m;
day = d;
year = y;
}
}
Question 10: Suppose that syllabus is a variable of type TreeMap, the keys in the map are
objects belonging to the Date class from the previous problem, and the values are of type
String. Write a code segment that will write out the value string for every key that is in the
month of September, 2002.