CS1713 Exam: Java Basics, Exams of Computer Science

The cs1713 exam questions and answers related to java basics, including true or false questions, term descriptions, java syntax understanding, and code execution. It covers topics such as class, instantiation, constructor, encapsulation, static fields, and more.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-w8e
koofers-user-w8e ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Your Name:
CS1713 โ€“ First Exam
Jun 24th, 2009, 12โ€“1pm
1. True or false? Mark as โ€œTโ€ or โ€œFโ€. (20 points)
(a) Java is case sensitive.
(b) Only letters and digits are allowed in variable names.
(c) All the names (class name, method name, . . .) have the same allowed characters.
(d) Class names must start with an uppercase letter.
(e) A field declaration follows the form private [TYPE] [NAME] = [INIT].
(f) Java knows primitive types and object (or reference) types.
(g) Integer types are made up of byte,short and int.
(h) float and double are used to store decimal numbers.
(i) String and char are used to store character strings.
(j) All other types not listed are object types.
(k) To instantiate a class, you use the keyword New.
For the next two questions, assume the methods are inside a class with a field named x.
(l) The following method is an accessor:
public int getX() {
return x+1;
}
(m) The following method is a mutator:
public void setXPlusOne(int x) {
x = x+1;
}
(n) A constructor follows the declaration rules of normal methods.
(o) You always need to implement a constructor.
(p) Fields are categorized as dynamic or static.
(q) Static fields are used for constant values.
(r) final variables and fields need to be initialized when declared.
(s) Java convention says that final field names should be written with the first
letter capitalized.
(t) Method overloading describes the ability to have multiple methods with the
same name.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download CS1713 Exam: Java Basics and more Exams Computer Science in PDF only on Docsity!

Your Name:

CS1713 โ€“ First Exam

Jun 24th, 2009, 12โ€“1pm

  1. True or false? Mark as โ€œTโ€ or โ€œFโ€. (20 points) (a) Java is case sensitive. (b) Only letters and digits are allowed in variable names. (c) All the names (class name, method name,... ) have the same allowed characters. (d) Class names must start with an uppercase letter. (e) A field declaration follows the form private [TYPE] [NAME] = [INIT]. (f) Java knows primitive types and object (or reference) types. (g) Integer types are made up of byte, short and int. (h) float and double are used to store decimal numbers. (i) String and char are used to store character strings. (j) All other types not listed are object types. (k) To instantiate a class, you use the keyword New. For the next two questions, assume the methods are inside a class with a field named x. (l) The following method is an accessor: public int getX() { return x+1; }

(m) The following method is a mutator: public void setXPlusOne(int x) { x = x+1; }

(n) A constructor follows the declaration rules of normal methods. (o) You always need to implement a constructor. (p) Fields are categorized as dynamic or static. (q) Static fields are used for constant values. (r) final variables and fields need to be initialized when declared. (s) Java convention says that final field names should be written with the first letter capitalized. (t) Method overloading describes the ability to have multiple methods with the same name.

  1. In the following code, show your understanding of the basic Java syntax. (20 points)

(a) On the right margin, write down what type of declaration (if any) was made in the line. (b) On the left margin, mark the lines that contain comments. (c) Underline (with a straight line) all instance field declarations. (d) Underline (with a wavy line) all variable and field uses. (e) Box all names that do not follow Java conventions or good style in general.

public class Bankaccount { /** Contains the current balance. / private double balance = 0; /* Contains the per-anno interest. */ private double Apy = 0; public Bankaccount(double Apy) { this.Apy = Apy; } public double getBalance() { return balance; } public double comp(int years) { double newBal = balance; for(int i = 0 ; i < years ; i++) { newBal = newBal * (1 + Apy / 100); } return newBal; // return value } }

  1. Execute the given code and write down/draw what is asked in the sub-questions.

(a) Write down the value of the expression. (5 points) i. 1 + 1.5 + " Hello"

ii. 2 / 1 + 2 / 2 + 2 / 3 + 2 / 4

iii. - ( 5 + Math.sqrt( 9 ) ) / ( 32 / 2 )

iv. "Hello " + 1 + 1.

v. 16 / 4 / 2

(b) Write down the console output. (5 points) i. int i = 5; if (i / 3 > 1) { System.out.println("a"); } else { System.out.println("a"+3*i); }

ii. int j = 1; double x = 16; do { x = Math.sqrt(x); j--; } while (j>1); System.out.println(x+" "+x*x);

iii. int s = 1; int i = 0; while(i<4) { i++; if (i%2==0) { s += i; } else { s -= i; } } System.out.println(i);

(c) Given the following class definitions, draw a memory diagram for the program state after the given code has executed. (10 points) public class X { public class Y { public Y x; public double x = 3; public double y = 2; public X y; } } i. int i = 15; X x = new X(); Y y = new Y(); double d1 = X.y + Y.x;

ii. X x = new X(); Y y = new Y(); x.y = 1.5; y.x = 4.5; x.x = y; y.y = x; double d2 = X.y + X.x.x;

  1. Use the software development model to design and implement a class.

(a) Underline important parts in the definition. Also mark the type of the part in the later design & implementation.

A number utility has an associated number. The number

can be retrieved as well as set. The utility can compute

the sum of the natural numbers up to and including the

associated number. The utility can compute the factorial

of the associated number.

(b) Draw a UML diagram of the class(es) you derive from the specification above.

(c) Implement the class(es).