Student Suggested Questions for the First Test
From MWCSWiki
Chapter 1
- If the following code: System.out.println("Hello, World!) appears i a program an will an error occur? If so, what type? (Ans: compile-time error, syntax error.)
- A sequence of characters enclosed in a quotation marks is called a _______ (string)
- Will following compile? Will it run?
<pre> public class HelloPrinter {
public static void Main(String[] args)
{
System.out.println("Hello, World!");
}
}</pre> Answer: It will compile fine, but when you try to run it, it will not run because Main is written with an upper-case M, and Java is case-sensitive.
- Write the statement(s) necessary to display
<pre>trapped in the matrix</pre> Possible answer: <pre> System.out.println("trapped ");System.out.println("in "); System.out.println("the ");System.out.println("matrix ");</pre>
- What type of programming language is Java? Ans - object-oriented
- System.ouch.println("My small intestine!") will result in what kind of error? Ans: syntax error
- What is the difference between a run-time error and a compile-time error?
Ans: A run-time error is one that occurs when the program is running or executing. An error in a program that causes execution of the program to be terminated. These are caused by misuses of the language that the compiler cannot detect and so are detected only when the program is run. They might include sending messages to null, dividing integers by zero, trying to access non-existent elements of an array, etc. A syntax error: (an error of language resulting from code that does not conform to the syntax of the programming language) "syntax errors can be recognized at compilation time";
Chapter 2
- Suppose we have the statement JFrame frame = new JFrame; What statement would we use to set the size of the frame to 600 by 400? What statement would we use to set the title of the JFrame object to "Test Question"?
Ans: frame.setSize(600,400). frame.setTtitle("Test Question").
- In Java newRectangle() is an example of:
<pre> a. method b. parameter c. constructor d. a & c </pre> Answer = d.
- Which type of method changes the state of its implicit parameter? Ans: mutator method.
- What is wrong with the statement String greeting = 13; (Ans: types don't match)
- Give the rules for a properly forming an identifier. (Ans: first character is a letter or underscore, followed by 0 or more letters, digits, underscore or $
- Describe the effect of the following statement:
<pre>Rectangle box = new Rectangle(15,25,30,35);</pre>
- Creating more than one method with the same name in the same class is called ______. Ans overloading.
Chapter 3
- For the code: Rectangle body = new Rectangle(10,10,20,20): What location do the coordinates represent on the rectangle? (Ans: 10,10 represents the coordinates of the top left corner of the rectangle. the first 20 represents the width and the second 20 represents the height of the rectangle.)
- An object stores its data in its ______ _____ (instance fields)
- In the method public void whatEver(double googoo), what is the access specifier? (public)
- What is wrong with the following class?
<pre> public class BankAccount {
public BankAccount() { }
private String owner;
}</pre> Answer: The variable owner is initialized to null, and we cannot use it without giving it a value. A good place to give it a value is in the constructor.
- Explain the difference between implicit and explicit parameters and give an example. Ans: the implicit parameter is the object that invoked the method. Explicit parameters are the values passed to the method.
- How do we refer to the implicit parameter in a method? Ans: this
Chapter 4
- Give an example of how to use a cast.
<pre> Answer: double balance = 1.2;
int dollars = (int)balance;
</pre>
- To read input from the keyboard you construct a
<pre> a. CashRegister = new CashRegister. b. Scanner in = new Scanner(System.in); after importing java.util.scanner c. Scanner in = new Scanner(System.in); d. none of the above. </pre> Answer = b.
- A ______ variable is a constant. (Ans: final)
- What operator computes the remainder of a division? (Ans: %)
- What is the first position of a string? (Ans: 0)
- Name the primitive types. See the text for the answers.

