CSC120: Exam 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What printf format specifier is good for money values?

$%.2f

Draw a picture of the array data structure created by this code: int[][] myData = new int[4][]; int row,column; for (row = 0; row < 4; row++) { myData[row] = new int[row+1]; for (column = 0; column <= row; column++) { myData[row][column] = row+column; } }

...

Given the code long[] start = {1,2,3,4,5,6,7}; long[] middle; long[] end; write lines of code so that middle refers to the same data in memory as start, and end refers to a new area of memory containing the same array values as start. Bonus marks if you create and set the values of end in a single simple statement that copies start.

...

How many lines of output does the following code segment produce? int whileIndex,forIndex; whileIndex = 1; while (whileIndex < 8) { System.out.println(whileIndex); for (forIndex = -1;forIndex < whileIndex;forIndex = forIndex+2 { System.out.println(forIndex); } }

...

What is the output from this program [an error occurred while processing this directive]

...

Which of the Java loop statements are pre-test, and which are post-test?

...

Write a method called getChoice that uses a do-while loop to prompt the user for a character, reads a character, and loops if the character is not upper case.

...

Write a method that receives a character and integer parameter, and returns a boolean indicating whether or not the ASCII value of the character is greater than the integer.

...

Write a one line declaration that creates and initializes an array of characters, with the elements initialized to the vowels.

...

Write a switch statement that increments, decrements, or zeroizes the int variable mover, depending on the character in the choice variable - one of 'I', 'i', 'D', 'd', 'Z', or 'z', indicating the first letter of the operation. If the character is none of those, print an error message.

...

Write an algorithm that can be used to help decide which type of loop statement to use for a given situation.

...

Write an if-else statement using a nice formatting style. Now write it again in another nice style. Now write it with bad formatting, and feel bad about doing it.

...

Write code to declare, create, and initialize all elements to 0, an array of MAX_SIZE integers.

...

Write method that receieves an array of double as its parameter and initializes all of the elements to 0.0.

...

Write a program that inputs a percentage, then prints out either pass or fail depending on whether or not the percentage is greater or equal to 50%. What is the output from this program?

... ...Program to be announced

What is the hanging else problem?

...?

What are three reasons for using methods?

1. to reuse code in a program or other programs without the need to retype it 2. perform specific tasks without needing to return anything 3. creating a clean, readable, and decipherable code instead of lumping everything in the main method

What is the exact output from the following (use an upsidedown triangle to indicate a space in the output): int birthday = 27; double catch22 = 22.22; long fibonacciLimit = 95; System.out.printf("%-8.0f %%%+d\n%5d\n",catch22,birthday,fibonacciLimit);

22 %+27 95

What is the use of a pre-test vs a post-test loop in Java?

A pre-test loop is used for something that may be done zero times. A post-test loop is used for something that is always done at least once.

Explain the relationship between a structure chart and the methods that may be used in a program.

A structure chart can be used to guide the development of code with methods: - The root node is the main method, - Internal nodes are separate methods - Leaf nodes are code within methods

Who was the first programmer?

Ada Lovelace

What is the maximum number of dimensions an array may have in Java?

Java does not limit the number of dimensions, but the Java VM spec limits the number of dimensions to 255.

How many values can a Java method return? Who made that Java design decision? What should be done to that person?

Methods can return only one data value., ..., ...,

What will give you warts?

Non-final public static data

What company developed Java?

Sun Microsystems (but now owned by Oracle)

What is the output from the following: height = 10; System.out.println("The cat " + (height > 10 ? "flew" : "sat") + " on the\b big\nmat");

The cat sat on thebig mat

Name the six primitive numeric data types in Java.

byte, short, int, long, float, double

What two types of things are classes and objects composed of?

methods, data values

How many public classes should there be in a file containing the main method?

there must be one public class per file and one public class (i.e. one file) must have the main method

How could the previous expression - 5+2*11 - be modified to make the result 77?

modify 5+2*11 -> (5+2)*11 Precedence Rules: (5+2)*11 (7)*11 -> 7*11 = 77

What are methods?

named segments of code

What feature allows multiple methods to have the same name?

overloading

Are strings primitive or reference data values?

primitive data values

What are the two different types of data in Java?

primitive, reference

If a method has seven formal parameters, how many actual parameters must be provided in a call to that method?

seven

How does Java decide which version of an overloaded method to execute?

different formal parameters: when such a method is called, the appropriate method is used dependent on the actual parameters.

What effective differences do the declarations double[] rainfall = new double[12]; and double rainfall[] = new double[12]; have?

the brakes are in different places. An array is like an object.

What is the effect of the break statement in a switch statement?

the break statement causes the program to execute the next statement outside the switch statement

Describe the naming convention for Java files.

file name ends with .java, and has one public class (has same name as file, and has the main method)

List all the reserved keywords of Java that are used in if-else and switch statements.

if-else statement: if, else, else if switch statement: case, switch, default, break

Rewrite the following while loop as a for loop: int loopIndex; loopIndex = 27; while (loopIndex > 3) { System.out.println(loopIndex); loopIndex -= 3; }

int loopIndex; for (loopIndex = 27; loopIndex > 3; loopIndex++) { System.out.println(loopIndex); loopIndex -= 3; }

What is a "one off" error?

a logic error involving the discrete equivalent of a boundary condition. It often occurs when a programming mistake is made like using <= when < should have been used in a comparison.

When are local variables in a method created and destroyed?

local variables are created each time the method runs and destroyed when the method returns

Describe the naming convention for Class identifiers.

are noun, and start with upper case

Describe the naming convention for Methods.

are verbs, and start with lowercase

Do if and if-else true and false blocks have to be compound statements?

yes

Are arrays primitive data types in Java?

No, arrays aren't primitive data types in Java. Arrays are container objects which are created dynamically. And arrays are thus considered reference data types.

Can methods call other methods? Can a method call itself?

Yes., Yes., (Methods can call other methods but a method can also call itself.)

Why is the result of the following 27? 5+2*11 ?

Precedence Rules: 5+2*11 5+22 = 27

How long does it take to execute an infinite loop? How long does it take if you have a faster CPU?

- An infinite loop will execute until you reboot or kill/stop the process. - irrelevant, how long does it take your patience to wear out?

What are local variables? Where can they be used?

- a variable that is declared within a method, - only in the method in which they're declared

Write a method that receives an integer parameter and returns a three dimensional array of characters, where each dimension is the size of the integer parameter (i.e., the array is a cube). What is the output from this program

-... -...

Given a problem, what steps are used to produce a computed solution?

0. Problem 1. Analysis and Design (Algorithms) 2. Programming (Programs) 3. Compiling and Linking (Compiled Programs) 4. Loading or Interpreting (Operating Systems -> Computers) 5. Use (Resources) or 0. Problem 1. Natural language, specification language -> computer scientist 2. Structure chart, algorithm -> programmer 3. High level language, e.g., Java -> compiler, Javac (machine code) 4. Machine Code -> Direct; or (Java) Byte Code -> Interpreter Java (machine code) 5. Computer execution

Describe the three forms of comments in Java.

1. a sequence of text that begins with the marker /* and terminates with another marker */ 2. double slashes //, used for single-line comment. Any text between the double-slash marker and the end of a line is a comment. 3. Javadoc: a specialized comment that can appear before the class declaration and other program elements, begins with /** marker and ends with the */ marker.

How are characters represented in the memory of a computer?

ASCII (American Standard Code for Information Interchange)

Who was the "father of computing"?

Charles Babbage

What was the name of the first completely electronic computer?

ENIAC 1

Explain the differences between high-level, byte code, and machine-code languages.

High-level language: - humans can understand but computers can't - provides a very high conceptual model of computing Byte code language: - interpreted language - interpreted into machine code by an interpreter Machine-code language: - what computers understand - written in 1s and 0s, binary

What are the formal and actual parameters of a method?

The formal parameters defined in the method header are local variables. Formal parameters receive copies of the actual parameters passed in the call.

When do formal and actual parameters have to have the same names?

Trick question formal and actual parameters do NOT have to have the same name, but they CAN have the same name

What are the scopes of: a) Formal parameters, b) Local variables, c) final class variables,

a) are visible from the method's opening { to the matching closing } b) are visible from the point of declaration to the matching closing } c) are visible throughout the class

What are the escape characters for: a. Tab b. Backspace c. Backslash d. Newline

a. \t b. \b c. \\ d. \n

What numeric data type would be most appropriate to store: a. An amount of money, b. The number part of a UM student number, c. The day number in a year

a. double b. int c. byte

What is the data type of each of these expressions: a. 2 * 2.0f * 2.0 b. (long) -2 * 2.0 c. (byte) 2 * (byte) 2 d. 2 * 2L

a. double b. long c. byte d. float

What is the data type of each of the following: a. 234.567 b. 0L c. (short)234 d. 2e2f

a. double b. long c. short d. float

What are import statements used for?

allows program to use classes and their instances defined in the designated package

Describe the naming convention for Final data values.

identifiers (including object identifiers) start with lower case

Describe the naming convention for Non-final data values.

identifiers (including object identifiers) start with lower case

What Java operator is used to create objects?

new operator

Explain the difference between the static and non-static parts of a class.

static parts exist independently, while non-static parts define what objects in the class look like

What is the main difference between a static and non-static data value?

static parts exist independently, while non-static parts define what objects in the class look like

What is "short circuit evaluation" of Boolean expressions?

when evaluating Boolean expressions (logical AND and logical OR) you can stop as soon as you find the first condition which satisfies or negates the expression.


Kaugnay na mga set ng pag-aaral

CHAPTER 54 MUSIC QUIZ, Chapter 53: MUS 110, Chapter 52: MUS 110, Music Test 2

View Set

BIOL252 PhysioEx 1: Ex 1 Cell Transport Mechanisms & Permeability

View Set

Financial Risk Management - Chapter 17

View Set

American Presidency Exam 1 Study Questions

View Set