Questions on Introduction to Computing in Java - Exam | CSC 116, Exams of Java Programming

Material Type: Exam; Class: Introduction to Computing - Java; Subject: Computer Science; University: North Carolina State University; Term: Spring 2005;

Typology: Exams

Pre 2010

Uploaded on 03/11/2009

koofers-user-3xy
koofers-user-3xy ๐Ÿ‡บ๐Ÿ‡ธ

5

(2)

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSC 116 โ€“ 002 Exam 1 Spring 2005
Name: ________________________________________________________________
Lab # and/or Lab Instructor: ________________________________________ (1 point)
This exam is worth 100 points. You may use your personal copy of the Wu textbook
and your personal notes. You many not use any other materials, including your
neighbor. You may not share your textbook, notes, or calculators.
Part 1: Vocabulary (20 points)
Syntax 1. Structure or pattern of a programming language.
Instance variables 2. Used to define the states or data of an object.
Class 3. A template of something you are trying to represent.
Constructor 4. Initializes an object when it is created.
Source code 5. Human readable code.
Methods 6. Used to define actions or behaviors of an object.
Integers or ints 7. A primitive data type for numbers that does not have a fractional
part.
Object 8. An instance of a class
Keyword 9. A word that has special meaning in a programming language
and cannot be used by programmers as a name for classes,
methods, or variables.
Compiler 10. Translates source code into machine code
pf3
pf4
pf5
pf8

Partial preview of the text

Download Questions on Introduction to Computing in Java - Exam | CSC 116 and more Exams Java Programming in PDF only on Docsity!

CSC 116 โ€“ 002 Exam 1 Spring 2005

Name: ________________________________________________________________

Lab # and/or Lab Instructor: ________________________________________ (1 point)

This exam is worth 100 points. You may use your personal copy of the Wu textbook

and your personal notes. You many not use any other materials, including your

neighbor. You may not share your textbook, notes, or calculators.

Part 1: Vocabulary (20 points)

Syntax 1. Structure or pattern of a programming language.

Instance variables 2. Used to define the states or data of an object.

Class 3. A template of something you are trying to represent.

Constructor 4. Initializes an object when it is created.

Source code 5. Human readable code.

Methods 6. Used to define actions or behaviors of an object.

Integers or ints 7. A primitive data type for numbers that does not have a fractional

part.

Object 8. An instance of a class

Keyword 9. A word that has special meaning in a programming language

and cannot be used by programmers as a name for classes, methods, or variables.

Compiler 10. Translates source code into machine code

Part 2: Primitive Data Types (20 points)

Arithmetic Operations (5 points)

You have three variables listed below. Compute the results of using these values in the following expressions. The values of these variables will not change through the

computation of the following expressions (a will always have the value 42).

int a = 42;

int b = 7; double c = 4.0;

  1. a + b = 49
  2. b * c = 28.
  3. a / c = 10.
  4. a / b = 6
  5. a % b = 0

Write Your Own Expressions

You are given two variables that contain the two short sides of a right triangle. Assume

they have been initialized to some appropriate value.

double sideA;

double sideB;

Provide Java statement(s) to calculate and store the area of the triangle. Remember

that the area of a triangle = (1/2) * base * height and that this is a right triangle. Also

remember to declare the variable that you will store the area in. (5 points)

double area = 0.5 * sideA * sideB;

Provide Java statement(s) to calculate and store the length of the 3 rd side of the

triangle. Remember that the Pythagorean Theorem is a 2

  • b 2 = c 2

. (5 points)

double sideC = Math.sqrt(sideA * sideA + sideB * sideB);

double sideC = Math.sqrt(Math.pow(sideA,2) + Math.pow(sideB,2));

c a

b

Part 4: Classes (30 points)

Writing a Class

You want to write a class to help a non-profit organizations keep track of the number of donations they have received. The NonProfitOrg class knows the name of the

organization, the total amount of money donated to the organization, and the number of

donors (both companies and individuals).

public class NonProfitOrg {

/**

  • Instance variables for the NonProfitOrg class that store
  • store the name of the organization, the total amount of
  • money donated to the organization, and the number of
  • donors. Please make sure to specify the correct access
  • identifier. */

private String orgName; private double money; private int numDonors;

  • Constructor for the NonProfitOrg class that provides
  • initial values for each of the instance variables. This
  • is a complete constructor. */

public NonProfitOrg(String name, double money, int numD) { this.orgName = name; this.money = money; this.numDonors = numD; //Make sure to watch for this being used if the param //and instance variable have the same name }

  • This method accepts a new donation. The money from the
  • donations is added to the total amount of donated money
  • and the number of donors is incremented by 1 */

public void acceptDonation( double money ) {

this.money += money; numDonors++;

//This method may have a parameter increasing the //number of donors as well โ€“ anything that //looks right

  • This method returns the average amount of money donated
  • across all donors. The average may contain a fraction
  • part. */

public double averageDonations() {

double average = this.money / this.numDonors; return average;

//If parameters are used correctly throughout the //whole example class then no points taken off

Part 5: Conditionals and Loops (20 points)

While Loops (5 points)

Write a while loop to calculate 3 7 .

int power = 1 ;

int counter = 1 ;

while(counter <= 7 ) {

*power = 3 ;

counter++ ;

}

For Loops (5 points)

Write a for loop to calculate 3 7 .

int power = 1 ;

for(int i = 1 ; i <= 7 ; i++) {

*power = 3 ;

}

Nested Loops (10 points)

Write a nested loop that produces the following output:

for(int i = 1 ; i <= 4 ; i ++ ) {

for(int j = 1 ; j <= i ; j ++ ) {

System.out.print( j );

}

System.out.println(โ€œโ€);

}