Final cosc

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

What is a type, as this term relates to programming?

A "type" represents a set of possible values. When you specify that a variable has a certain type, you are saying what values it can hold. When you say that an expression is of a certain type, you are saying what values the expression can have. For example, to say that a variable is of type int says that integer values in a certain range can be stored in that variable.

What is a variable? (There are four different ideas associated with variables in Java. Try to mention all four aspects in your answer. Hint: One of the aspects is the variable's name.)

A variable is a memory location that has been given a name so that it can easily be referred to in a program. The variable holds a value, which must be of some specified type. The value can be changed during the course of the execution of the program.

What is the main difference between a while loop and a do..while loop?

Both types of loop repeat a block of statements until some condition becomes false. The main difference is that in a while loop, the condition is tested at the beginning of the loop, and in a do..while loop, the condition is tested at the end of the loop. It is possible that the body of a while loop might not be executed at all. However, the body of a do..while loop is executed at least once since the condition for ending the loop is not tested until the body of the loop has been executed.

What is the difference between a "compiler" and an "interpreter"?

Compilers and interpreters have similar functions: They take a program written in some programming language and translate it into machine language. A compiler does the translation all at once. It produces a complete machine language program that can then be executed. An interpreter, on the other hand, just translates one instruction at a time, and then executes that instruction immediately. (Java uses a compiler to translate java programs into Java Bytecode, which is a machine language for the imaginary Java Virtual Machine. Java Bytecode programs are then executed by an interpreter.)

Explain the difference between high-level languages and machine language.

Programs written in the machine language of a given type of computer can be directly executed by the CPU of that type of computer. High-level language programs must be translated into machine language before they can be executed. (Machine language instructions are encoded as binary numbers that are meant to be used by a machine, not read or written by people. High-level languages use a syntax that is closer to human language.)

If you have the source code for a Java program, and you want to run that program, you will need both a compiler and an interpreter. What does the Java compiler do, and what does the Java interpreter do?

The Java compiler translates Java programs into a language called Java bytecode. Although bytecode is similar to machine language, it is not the machine language of any actual computer. A Java interpreter is used to run the compiled Java bytecode program. (Each type of computer needs its own Java bytecode interpreter, but all these interpreters interpret the same bytecode language.)

Show the exact output that would be produced by the following main() routine: public static void main(String[] args) { int N; N = 1; while (N <= 32) { N = 2 * N; System.out.println(N); } }

The exact output printed by this program is: 2 4 8 16 32 64 (The hard part to get right is the 64 at the end. The value of N doubles each time through the loop. For the final execution of the loop, N starts out with the value 32, but N is doubled to 64 before it is printed.)

Write a for loop that will print out all the multiples of 3 from 3 to 36, that is: 3 6 9 12 15 18 21 24 27 30 33 36.

int N; for ( N = 3; N <= 36; N++ ) { if ( N % 3 == 0 ) System.out.println( N ); }

Suppose that s1 and s2 are variables of type String, whose values are expected to be string representations of values of type int. Write a code segment that will compute and print the integer sum of those values, or will print an error message if the values cannot successfully be converted into integers. (Use a try..catch statement.)

try { int n1, n2; // The values of s1 and s2 as integers. int sum; // The sum of n1 and n2. n1 = Integer.parseInt(s1); n2 = Integer.parseInt(s2); sum = n1 + n2; // (If an exception occurs, we don't get to this point.) System.out.println("The sum is " + sum); } catch ( NumberFormatException e ) { System.out.println("Error! Unable to convert strings to integers.); }


Kaugnay na mga set ng pag-aaral

A&P Chapter 14 The Autonomic Nervous System and Homeostasis

View Set

Физика, Кинематика, 9 класс

View Set

Chapter 4: Forces and Newton's laws of motion, Mastering Physics 4, physics exam 2

View Set

Chapter 8 - Designing Pay Levels

View Set