







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Your Name:
Jun 24th, 2009, 12โ1pm
(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.
(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 } }
(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;
(a) Underline important parts in the definition. Also mark the type of the part in the later design & implementation.
(b) Draw a UML diagram of the class(es) you derive from the specification above.
(c) Implement the class(es).