期末答案

期末ç­...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ICSI201 Fall 2008 Final Exam Solution Key Page 1 of 7 The main method in Java is declared public public static void main(String args) {} but most fields in classes are declared private private int age; private Picture pictureImageArrary; 1. Explain why the main method needs to be declared public it needs to be declared public so that it can be called by java as the master method So you can test or run the class Many of you responded that the main method was declared public so that other classes could call it thats why methods in general are declared public but the main method is a special case it would rarely, if ever, be called by another class. Its the master method that Java executes when you ask it to run a particular class. If it werent public, then java wouldnt be able to run the class. 2. Explain why fields are usually declared private. So that they can only be accessed by code within the same class or through a getter/setter accessor/modifier method Fields are variables declared within the class, but outside of any method. Usually, they are private so that the class can control access to them and ensure that the always have valid values. 3. Correct the following code so that it is valid Java: System.out.println("He said, "hello, world!""); The goal here was to produce the intended output by eliminating the syntax error. The source of the error is the repeated use of double quotes. Double quotes are used in Java to delimit the beginning and ending of string literals (strings of characters). You cant have a naked (unescaped) double quote inside a string that is delimited by double quotes: // invalid The solution is to escape the double quotes that are meant to be taken literally, i.e. that are meant to be printed as part of the string: \ \ // valid The two outer double quotes now delimit the string while the inner double quotes, now escaped by prepending them with a backslash, are simple printed. System.out.println("He said, \"hello, world!\""); System.out.println("He said, 'hello, world!'"); // another possible approach System.out.println("He said, " + '"' + "hello, world!" + '"' ); ICSI201 Fall 2008 Final Exam Solution Key Page 2 of 7 4. To find the number of items in an array in Java, you use arrayName .length length, in this context, is a: a) public method b) public field c) private method d) private field e) constructor All methods have parentheses following them we said this in class hundreds of times! So, options a and c are excluded. A constructor is a special method that is called when a new instance of a class is created, but it is a method, so option e is also excluded. That leaves options b and d . If length were private, we wouldnt be able to write core, like the above, that accesses it directly. That eliminates option d . Therefore, length is, and must be , a public field; option b ....
View Full Document

This note was uploaded on 01/17/2009 for the course CSI 201 taught by Professor Doanne during the Fall '08 term at SUNY Albany.

Page1 / 7

期末ç­...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online