Docsity

Java Programming: Compiling, Execution, and Errors, Exams of Computer Science

Toggle sidebar
bg1
1.1. The Java Compiler and the Java Virtual Machine
1. State, in your own words, the meaning of the following terms:
a) Java is case sensitive b) source code
c) compiling d) class file
e) JDK f) JVM
g) default program entry point h) platform independence
2. Describe, in your own words:
a) the framework for every basic executable Java program
b) the significance of curly brackets
c) why Java source code should be indented properly
d) what happens when compiling source code that contains errors
e) how to compile a Java program
f) how to execute a Java program
g) what the advantages are of Java over other programming languages
3. What is the compiler error message, if any, if you
a) compile a class (program) that does not contain any body, i.e. a source code that looks like public
class Test { }
b) compile a program without the standard main method
c) compile a program that contains two standard main methods
d) compile a program which has been saved using a name different from the name of the program
e) create a standard program containing one line with two statements
System.out.println("Something"), where each one is properly terminated by a semicolon
f) create a standard program containing two lines with statements System.out.println("Something")
but neither statement is terminated with a semicolon
g) create a class containing the standard main method with one System.out.println statement and
proper semicolons, but everything is listed in one single (long) line
h) create a standard program containing one or more System.out.println statements, but using either
single quotes or no quotes at all instead of double quotes inside each statement
i) create a standard program containing the standard main method but inside that method you misspell
System.out.println in some way
4. Describe the complete process of creating and executing a working program. In other words, how is the
source code file created, what names must be used, how is the class file created, and how is the class file
executed.
1.2. Data Types, Assignments, and Arithmetic
1. State, in your own words, the meaning of the following terms:
a) int, long, short b) double, float
c) char and boolean d) largest and smallest value
e) literal f) binary representation
g) bits and bytes h) declaration of a variable
i) reserved keywords j) assignment operator
k) initializing a variable l) basic arithmetic operators
m) order of precedence n) Java trig functions
o) ++, --, +=, *=, /=, -=, %= p) Java math constants
q) boolean operators r) typecasting
s) comparison operators
2. Describe, in your own words:
a) where in a program variables must be declared
b) how variable names can look like, and how variable names should look like
c) which side of an assignment is evaluated first
d) what the "percent" operator % does
e) what the resulting type is when dividing two int, or an int and a double, or two double
f) what the resulting type is when adding two int, or an int and a double, or two double
3. What is the exact syntax to:
a) declare one variable of type double
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Java Programming: Compiling, Execution, and Errors and more Exams Computer Science in PDF only on Docsity!

1.1. The Java Compiler and the Java Virtual Machine

1. State, in your own words, the meaning of the following terms:

a) Java is case sensitive b) source code

c) compiling d) class file

e) JDK f) JVM

g) default program entry point h) platform independence

2. Describe, in your own words:

a) the framework for every basic executable Java program

b) the significance of curly brackets

c) why Java source code should be indented properly

d) what happens when compiling source code that contains errors

e) how to compile a Java program

f) how to execute a Java program

g) what the advantages are of Java over other programming languages

3. What is the compiler error message, if any, if you

a) compile a class (program) that does not contain any body, i.e. a source code that looks like public

class Test { }

b) compile a program without the standard main method

c) compile a program that contains two standard main methods

d) compile a program which has been saved using a name different from the name of the program

e) create a standard program containing one line with two statements

System.out.println("Something"), where each one is properly terminated by a semicolon

f) create a standard program containing two lines with statements System.out.println("Something")

but neither statement is terminated with a semicolon

g) create a class containing the standard main method with one System.out.println statement and

proper semicolons, but everything is listed in one single (long) line

h) create a standard program containing one or more System.out.println statements, but using either

single quotes or no quotes at all instead of double quotes inside each statement

i) create a standard program containing the standard main method but inside that method you misspell

System.out.println in some way

4. Describe the complete process of creating and executing a working program. In other words, how is the

source code file created, what names must be used, how is the class file created, and how is the class file

executed.

1.2. Data Types, Assignments, and Arithmetic

1. State, in your own words, the meaning of the following terms:

a) int, long, short b) double, float

c) char and boolean d) largest and smallest value

e) literal f) binary representation

g) bits and bytes h) declaration of a variable

i) reserved keywords j) assignment operator

k) initializing a variable l) basic arithmetic operators

m) order of precedence n) Java trig functions

o) ++, --, +=, *=, /=, -=, %= p) Java math constants

q) boolean operators r) typecasting

s) comparison operators

2. Describe, in your own words:

a) where in a program variables must be declared

b) how variable names can look like, and how variable names should look like

c) which side of an assignment is evaluated first

d) what the "percent" operator % does

e) what the resulting type is when dividing two int, or an int and a double, or two double

f) what the resulting type is when adding two int, or an int and a double, or two double

3. What is the exact syntax to:

a) declare one variable of type double

b) declare several variables of type double in one line

c) declare one variable of type double and assign the value –17.3 to it at the same time

d) declare several variables of type double and assign values to them at the same time

4. What is the compiler error message, if any, if you

a) assign an int value to a double variable, or a double value to an int variable

b) assign a char value to an int variable, or an int value to a char variable

c) if you assign a numeric value to a boolean variable, or a boolean value to a numeric variable

5. Convert the numbers 321 and 33 to their binary representations, and the numbers (010010001) 2 and

(11011011) 2 to their usual representation.

6. How many bits and bytes are necessary to store all possible short values?

7. Write a program that has three double variables containing some values and prints to the screen the

mathematical sine of the first one, the cosine of the second one, and the tangent of the third number. Also

display the sum of the squares of sine and cosine of the three double numbers.

8. What is the value of z at the end of this code fragment?

int z = 23; int x = 7, y = 53; x *= y; y /= 2; x++; z += (x + y);

9. Determine if the following statements are true or false, using:

boolean q = true, r = false, s = true, result;

a) result = ( q || r );

b) result = ( q && s );

c) result = ( q || r || s );

d) result = ( q && r || s );

e) result = ( ( q || r ) && s );

f) result = ( ( ( q && r ) || s ) && q );

g) result = !( !( !r || s ) && ( !q || !s ) );

h) result = !( r || q && s ) || ( !q && ( !(r == s ) ) );

i) result = ( s || ( r && !q ) && ( q && !r || ( s && !( !q ) ) ) );

10. Determine if the following statements are true or false, using:

int x = 5, y = 9, z = 2; boolean r = false, result;

a) result = ( 5 >= x);

b) result = ( y < z );

c) result = (y == 9) && ( (x >= 2) || r );

d) result = !( ( r || ( y < x ) ) && ( 5 >= x ) );

e) result = ( ( x + y ) >= ( 16 – z ) );

f) result = ( x == ( (z++) + 2 ) );

g) result = ( x == ( (++z) + 2 ) );

h) result = ( !r && ( (--y) > (z + x) ) );

i) result = (((21 – z) == (((y++) + x + z) + 1)) && (!r == true));

1.3. Basic Program Control

1. State, in your own words, the meaning of the following terms:

a) code block b) simple if statement

c) if-else statement with alternative d) nested if-else-if statement

e) loop f) loop initialization, modification, and test

g) for loop h) while loop

i) a loop counter

2. Describe, in your own words:

double xx = 32; while (xx > 1.0); xx /= 2; System.out.println("xx = " + xx); int i = 10; int j = 4; int k = i / j; double x = i / j; double y = (1.0 * i) / (1.0 * j); System.out.println("k = " + k + ", x = " + x + ", y = " + y); } }

11. Write some Java code segments that solve the indicated tasks (you do not have to write a complete

program, a small code segment will suffice):

a) Code to decide whether an integer number is positive, negative, or zero

b) Code to print all integers from 1 to 50 on the screen

c) Code to print all positive odd integers between 1 and 50

d) Code to add all positive, even integers between 1 and 50

12. Assume that two double variables x and y have been declared and contain some unknown values. Write

some code that:

a) swaps both values so that the first contains the second value, the second one the first value

b) swaps values if necessary so that the first one contains the bigger of the two values

c) swaps values if necessary so that the first one contains the smaller of the two values

1.4. Strings

1. State, in your own words, the meaning of the following terms:

a) String b) String literals

c) String operations d) Concatenate

e) String index f) Dot operator

2. Describe, in your own words:

a) the difference is between "==" and ".equals" and "="

b) what a substring is

c) what white space is

3. What is the exact syntax to:

a) initialize a String (show both methods)

b) concatenate two or more strings

c) display a String

d) find the length of a String

e) compare two strings in their lexicographical ordering

4. What is the compiler message, if any, if you:

a) use "==" instead of "equals" when comparing String.

b) retrieve a character at an invalid position in a String.

c) retrieve a character in a String of length zero.

d) type the following statement: String s1 = "Hello" + " " world;

5. Create a program that displays a string in reverse order, one character per line, to the screen.

6. Create a program that takes a string "HeLlO wOrLd" and changes all lower case characters to uppercase,

and all uppercase characters to lowercase. You may want to use the toLowerCase() and toUpperCase()

operations, and maybe others as well.

7. Create a program called Encryptor, which takes a string of characters and displays for each character its

integer representation. Hint : use typecasting.

8. If s is a String containing a name in the format "first last", i.e. the first name, than a space, then the last

name (no middle initial), then break up that string into two variables at the space. The first one should

contain the first name, the second the last name.

2.1. Methods and Parameters

1. State, in your own words, the meaning of the following terms:

a) method b) method header and body

c) method parameter list d) return type

e) return

2. Describe, in your own words:

a) why methods are useful.

b) where a method starts.

c) where a method ends.

d) the difference between the body of a class and the body of a method.

3. What is the exact syntax:

a) to define a method

b) to define the standard main method

c) to define a method with no input parameters and no return type

d) to define a method with two integer parameters and no return type

e) to define a method with the three input parameter boolean, int, and double, and no return type

f) to define a method as in (c) but with int return type

g) to define a method as in (e) but with double return type

h) to call the methods in (c), (d), (e), (f), and (g)

4. What is the compiler error message, if any, if you:

a) return an int in a method defined as

public static boolean isPrime(int number)

b) return an int in a method defined as

public static double isFun(int number)

c) call the method defined as

public static int someMethod(boolean myNum, double yourNum)

using the statement:

someMethod(x, yourNum) where x is a char and yourNum is a double.

d) call the method defined in (c) using the statement:

someMethod(x, yourNum) where x is a boolean and yourNum is an int.

e) define the following method

public static void mYmEtHoD(int nUmBer; boolean sToP) { ... }

f) define the method

public static void anotherMethod() { ... }

and call it using the code fragment

anotherMethod(anotherMethod());

f) define the methods

public static void oneMethod(double x) { …} public static int anotherMethod() { return 10 }

and use them in a statement

oneMethod(anotherMethod());

f) define the methods

public static void oneMethod(int x) { …} public static double anotherMethod() { return 10.0 }

and use them in a statement

oneMethod(anotherMethod());

5. Write a complete program that contains a standard main method and at least two additional methods to

compute the area and circumference of a circle, where

 the main method contains the appropriate variable declarations

 output takes place in the main method or a separate method used by main

 the computations of area and circumference is handled in separate methods used by main

6. Create a method nextPrime that takes as input a positive integer and returns the next prime number after

the input value. Use that method to compute the average of the first 1,000 prime numbers (which is

different from the average of the prime numbers less than 1,000).

7. Create a program containing a method fahrenheitToCelsius to change a temperature in degrees

Fahrenheit to degrees Celsius. The parameter list should contain a double named fahrenheit. Add a

{ int i = 4; for (i = 2; i < 9; i++) System.out.println(i); System.out.println(a) System.out.println(i); public static workerAntScope() { System.out.println(i); } public static void main() { beatleScope(); antScope(); workerAntScope(); } }

5. Fix all the bugs in the above program and show where each field and variable is valid.

6. Consider the following methods

public static void Unknown(double x, double y) { x = x + y; y = x + y; } public static int Mystery(int x, int y) { x = x + y; y = x + y; return (x + y); }

What is the output if the above methods are used in a code segment as follows:

double x = 1.0; double y = 2.0; Unknown(y, x); System.out.println("x = " + x + ", y = " + y); i = 3; j = 4; j = Mystery(i, j); System.out.println("i = " + i + ", j = " + j);

7. Given the following program, what is the output?

public class FlavoredScope { public static int x = 4; public static int y = 8; public static int z = 2; public static void mints(int x, int z, int y) { winterfresh(y,z); x = pepermint(y); System.out.println(x); System.out.println(y); System.out.println(z); } public static void winterfresh(int x, int y) { int z = -1; x = y + z; } public static int peppermint(int y) { y *= z; return y; } public static int cherry(int y, int z, int x) { x = y; y = z ; z *= y; return z; } public static void main( String args[ ] )

{ mints(z, y, cherry(x, y, z)); System.out.println(x); System.out.println(y); System.out.println(z); } }

2.3. Arrays

1. State, in your own words, what is meant by the following terms:

a) array b) array index

c) length of an array d) declaring an array

e) initializing an array f) accessing array elements

g) arrays as input parameters h) implicitly initialized array

2. Describe, in your own words:

a) the usefulness of arrays

b) what happens when an element in an array is changed in a method

c) the difference between a String and an array of characters

3. What is the exact syntax:

a) to declare an array of five integers

b) to declare an array without initializing it

c) to declare an array containing the following three strings: “Java” “Is” “Fun”

d) to specify the size of an array that was not initialized

e) to get the value from an array element

f) to create a method with an array as input type

g) to call a method that uses an array as an input type

h) to find the length of an array

i) to obtain a random number

j) to obtain command line input for a Java program

4. What is the compiler error message, if any, if you:

a) access an element in an array with index greater then the length of the array

b) access an element in an array with index less than 0

c) access an element in an array that has not been initialized

d) put a character in an array of integers

e) put an integer in an array of doubles

f) put a double in an array of integers

5. If an array is of size 25, what are the index numbers to access the elements of the array?

a) 0-

b) 1-

c) 2-

d) 3-

e) 4-

6. Create a program that will fill an array of 100 integers with the numbers 1-100 in increasing order. Then

display all even numbers, odd numbers, and prime numbers in that array.

7. Consider the following code segment:

int A[] = new int[10]; A[0] = 1; A[1] = 2; for (int i = 2; i < A.length; i++) { if ((i % 2) == 1) A[i] = A[i-1] + A[i-2]; else A[i] = 2 * A[i-1]; }

Recall that (i % 2) returns 0 if i is even and 1 if i is odd. What is the value of A[4] after the above code

segment executes? How about A[9]? What about A[10]?

8. Some methods dealing with arrays are defined as follows:

public static int mystery(int a[]) { int answer = 0;