Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Variables, Operators, and Expressions Exercises, Assignments of Computer Science

A series of exercises related to java variables, operators, and expressions, including reserved keywords, variable declaration and initialization, arithmetic and tritigonometric functions, assignment and comparison operators, and code legality. It also includes exercises for determining the value of variables after certain operations and evaluating boolean expressions.

Typology: Assignments

Pre 2010

Uploaded on 08/08/2009

koofers-user-sh9
koofers-user-sh9 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exercises
1. State, in your own words, the meaning of the following terms:
a) reserved keywords b) declaration of a variable
c) initializing a variable d) assignment operator
e) order of precedence f) basic arithmetic operators
g) Java rounding functions h) Java trig functions
i) ++, --, +=, *=, /=, -=, %= j) Java math constants
k) boolean operators l) comparison operators
2. Describe, in your own words:
a) how variable names can look like, and how variable names should look like
b) which side of an assignment is evaluated first
c) what the "percent" operator % does
d) what the resulting type is when dividing two int, or an int and a double, or two
double
e) 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. Is the following code fragment legal? Why or why not?
int package = 10;
Is the following code fragment legal? Why or why not?
int Package = 10;
6. What is the value of z at the end of this code fragment?
int z = 3;
int x = 7, y = 5;
x *= y;
y /= 2;
x++;
z += (x + y);
7. Determine if the following statements are true or false, using:
pf2

Partial preview of the text

Download Java Variables, Operators, and Expressions Exercises and more Assignments Computer Science in PDF only on Docsity!

Exercises

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

a) reserved keywords b) declaration of a variable

c) initializing a variable d) assignment operator

e) order of precedence f) basic arithmetic operators

g) Java rounding functions h) Java trig functions

i) ++, --, +=, *=, /=, -=, %= j) Java math constants

k) boolean operators l) comparison operators

2. Describe, in your own words:

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

b) which side of an assignment is evaluated first

c) what the "percent" operator % does

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

double

e) 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. Is the following code fragment legal? Why or why not?

int package = 10;

Is the following code fragment legal? Why or why not?

int Package = 10;

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

int z = 3; int x = 7, y = 5; x *= y; y /= 2; x++; z += (x + y);

7. 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 ) ) ) );

8. 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));

9. 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. You do NOT have to submit this program, it’s only to

practice.

10. Write a practice program to compute the volume and surface area of a sphere of

radius r = 10. Make sure to name variables appropriately. Do the same for a box of a

given width, height, and length. You do NOT have to submit this program, it’s only

to practice.