0% found this document useful (0 votes)
122 views9 pages

Java Output and Identifier Evaluation

The document contains 10 multiple choice and true/false questions about Java identifiers, operators, expressions, and output statements. It also includes several code examples and exercises to evaluate variables and expressions in Java. The document provides 10 programming exercises involving variables, operators, expressions, and output statements in Java. It tests understanding of basic Java concepts like identifiers, data types, arithmetic and logical operators, and print statements. The exercises cover topics like valid identifier names, evaluating expressions, finding variable values after a sequence of statements, declaring variables, and using print statements to output values with different data types.

Uploaded by

Adonis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views9 pages

Java Output and Identifier Evaluation

The document contains 10 multiple choice and true/false questions about Java identifiers, operators, expressions, and output statements. It also includes several code examples and exercises to evaluate variables and expressions in Java. The document provides 10 programming exercises involving variables, operators, expressions, and output statements in Java. It tests understanding of basic Java concepts like identifiers, data types, arithmetic and logical operators, and print statements. The exercises cover topics like valid identifier names, evaluating expressions, finding variable values after a sequence of statements, declaring variables, and using print statements to output values with different data types.

Uploaded by

Adonis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ADONIS PADIWAN

BSMATH 3

MODULE 2
Seatwork 2

1. Mark the following statements as true or false.


a. An identifier can be any sequence of digits and letters.
FALSE

b. In Java, there is no difference between a reserved word and a predefined


identifier.
FALSE

c. A Java identifier can start with a digit.


FALSE

d. The operands of the modulus operator must be integers.


TRUE

e. If the value of a is 4 and the value of b is 3, then after the statement a = b;


the value of b is still 3.
TRUE

f. In an output statement, the newline character may be a part of the string.


TRUE

g. The following is a legal Java program: public class JavaProgram { public


static void main(String[] args) { } }
TRUE

h. In a mixed expression, all operands are converted to floating-point numbers.


FALSE

i. Suppose x = 5. After the statement y = x++; executes, y is 5 and x is 6.


TRUE

j. Suppose a = 5. After the statement ++a; executes, the value of a is still 5


because the value of the expression is not saved in another variable.
FALSE
2. Which of the following are valid Java identifiers?
a. myFirstProgram
b. MIX-UP
c. JavaProgram2
d. quiz7
e. ProgrammingLecture2
f. 1footEquals12Inches
g. Mike'sFirstAttempt
h. Update Grade
i. 4th
j. New_Student

3. Evaluate the following expressions.


a. 13 / 4
=3.25

b. 2 + 12 / 4
2+3
=5

c. 21 % 5
=2

d. 3 - 5 % 7
3–7
=4

e. 17.0 / 4
=4.25

f. 8 - 5 * 2.0
8.0-10.0
= - 2.0

g. 14 + 5 % 2 – 3
14 + 5 – 3
19 – 3
= 16

h. 15.0 + 3.0 / 2.0


15.0 + 1.5
= 10.0
4. Do a walk-through to find the value assigned to e. Assume that all variables are
properly declared.

a = 3;

b = 4;

c = (a % b) * 6;

d = c / b;

e = (a + b + c + d)/ 4;

a=3

b=4

c = (3 % 4) * 6
c = 75 * 6
c = 450

d = 450/4
d = 112.5
d = 112

e = (3 + 4 + 450 + 112) /4
e = 569/4
e = 142.25
e = 142

5. Which of the following variable declarations are correct? If a variable declaration is


not correct, give the reason(s) and provide the correct variable declaration.
n = 12; //Line 1 CORRECT

char letter = ; //Line 2 CORRECT

int one = 5, two; //Line 3 INCORRECT


It should be something like this:
int (1) = 1
int (5) = 5

double x, y, z; //Line 4 INCORRECT


The letters should be integers.

6. Suppose x, y, z, and w are int variables. What value is assigned to each variable after
the last statement executes?

x = 5;
z = 3;
y = x - z;
z = 2 * y + 3;
w = x - 2 * y + z;
z = w - x;
w++;

x=5 w=5–4+7
w=1+7
w=8
z=3
z=w–x
y=5–3 z=8–5
y=2 z=3
z = 2*2+3 w++
z=4+3 w=9
z=7
After the last statement executes, the
w=x-2*y+z value of x is 5, the value of y is 2, the
w=5–2*2+7 value of z is 3 and the value of w is 9.
7. Suppose x, y, and z are int variables and w and t are double variables. What is the
value of each variable after the last statement executes?

x = 17;

y = 15;

x = x + y / 4;

z = x % 3 + 4;

w = 17 / 3 + 6.5;

t = x / 4.0 + 15 % 4 – 3.5;

x = 17

y = 15

x=x+y/4
x = 17 + 15/4
x = 17 + 3
x = 20

w = 17.0 / 3.0 + 6.0


w = 5.0 + 6.0
w = 11.0

t = x / 4.0 + 15 % 4 – 3.5
t = (20.0/4.0) + (15% 4) – 3.5
t = 5.0 + 75.0 – 3.5
t = 80.0 – 3.5
t = 76.5

After the last statement executes, the value of x is 20, y is 15, w is 11.0 and t is 76.5
8. Suppose x and y are int variables and x = 25 and y = 35. What is the output of each of
the following statements?

a. [Link](x + ' ' + y);

b. [Link](x + " " + y);

a. The output is 60.

b. The output is 25 35

9. Suppose x, y, and z are int variables and x = 2, y = 5, and z = 6. What is the output of
each of the following statements?

a. [Link]("x = " + x + ", y = " + y + ", z = " + z);

b. [Link]("x + y = " + (x + y));

c. [Link]("Sum of " + x + " and " + z + " is " + (x + z));

d. [Link]("z / x = " + (z / x));

e. [Link](" 2 times " + x + " = " + (2 * x));

The outputs are:

a. x = 2, y = 5, z = 6

b. x + y = 7

c. Sum of 2 and 6 is 8

d. z/x = 4

e. 2 times 2 = 4
10. What is the output of the following statements? Suppose a and b are int variables, c is a
double variable, and a = 13, b = 5, and c = 17.5.

a. [Link](a + b - c);

b. [Link](15 / 2 + c);

c. [Link](a / (double)(b) + 2 * c);

d. [Link](14 % 3 + 6.3 + b / a);

e. [Link]((int)(c)% 5 + a - b);

f. [Link](13.5 / 2 + 4.0 * 3.5 + 18);

The outputs are: d.


14 % 3 + 6.3 + 5 / 13
a. 4 + 6.3
13 + 5 – 17.5 10.3
18 – 17.5
0.5 e.
((int)(17.5)% 5 + 13 – 5)
b. 17 % 5 + 13 – 5
15/2 + 17.5 4 + 13 – 5
7 + 17.5 17 – 5
24.5 12

c.
f.
(15.0 / 5.0) + (2.0*17.5)
(13.5 / 2) + (4.0 * 3.5) + 18
3.0 + 35.0
6.75 + 14.0 + 18
38.0
20.75 + 18
38.75
1. Write a program that produces the following output:

***************************************************

* Programming Assignment 1 *

* Computer Programming I *

* Author: Duffy Ducky *

* Due Date: Thursday, Jan. 24 *

**************************************************

{
public static void main(String[] args)
{
[Link]("***************************************************")
;
[Link](“* Programming Assignment 1 *”);
[Link](“* Computer Programming I *”);
[Link](“* Author: Duffy Ducky *”);
[Link](“* Due Date: Thursday, Jan. 24 *”);
[Link]("***************************************************")
;

}
}
2. Write a program that prompts the user to input the length and width of a rectangle and
then prints the rectangle’s area and perimeter.

{
static Scanner console = new Scanner([Link]);
public static void main(String[] args)
{
int length;
int width;

[Link]("Enter two values of the sides of a rectangle separated by


spaces.");
length = [Link]();
width = [Link]();
[Link]("The area of the rectangle is = " + (length * width);
[Link]("The perimeter of the rectangle is = " + (2 * (length + width));
}
}

Common questions

Powered by AI

A common mistake in Java variable declarations is using variable names that start with numbers or using invalid syntax like typing int (1) = 1; instead of simply declaring int one = 1;. Java variable names should begin with a letter or an underscore and not include special characters. To correct these, ensure variable names are alphanumeric strings where the first character is a letter or an underscore, and follow proper syntax .

Miscalculations in logical or arithmetic expressions in Java can lead to unexpected behavior and runtime errors. When expressions involve multiple data types, improper handling of type conversions can result in precision loss or semantic errors. For instance, integer division results in truncation, potentially leading to zero denominator issues or skewed mathematical results. Mixing floating-point and integer arithmetic without explicit conversions might also lead to subtle bugs or misinterpretations of data, thus affecting application reliability and accuracy .

Double variables cannot always be used in certain operations without explicit casting due to type precision and operation expectations. For example, certain integer operations expect integer results and throwing a double into the mix can result in a type mismatch error. Consequently, double-to-int operations necessitate explicit casting to signify a developer's awareness of potential data loss during conversion due to precision drop (e.g., fractional parts are truncated).

In Java, post-increment operations (e.g., x++) increase the variable's value by one after the current expression is evaluated. When a variable is post-incremented, its current value is used in the expression before it is incremented. For example, if x is 5 and the statement y = x++ executes, y is assigned the current value of x (5), and then x is incremented to 6. This behavior allows the expression to use the original value before the increment .

Errors in input, such as entering non-numeric values when numbers are expected, can lead to exceptions or incorrect results when computing the area of a rectangle in Java. For instance, if a user inputs non-integers for the sides of the rectangle, it will result in runtime errors unless the input is validated and handled correctly. This underscores the necessity of robust error-checking mechanisms to ensure correct program execution and valid outputs .

The operands of the modulus operator in Java must be integers. The modulus operator (%) is generally used for integer arithmetic to find the remainder of a division. This restriction aligns with Java's type safety and precision in integer arithmetic. Using the modulus operator with non-integer types would require additional type conversions, which might not preserve numeric accuracy .

In mixed expressions, Java does not automatically convert all operands to floating-point numbers because it adheres to a strict type conversion precedence. This involves promoting byte, short, and char to int, and then potentially converting int and long to double or float if the expression demands it due to float precision operations. Such conversions only occur when necessary to preserve precision in operations involving floating-point arithmetic; otherwise, integer precision is preserved .

In Java, reserved words have a special meaning and cannot be used as identifiers. They are part of the language syntax. Predefined identifiers, however, are identifiers that have been defined previously in a program or library but can be reused or redefined by the programmer. Therefore, while reserved words are fixed by the language specifications, predefined identifiers are flexible and can be altered or reassigned by the programmer .

Java identifiers must not start with a digit because the syntax rules of Java treat sequences starting with digits as potential numeric literals, not variable names. This prevents confusion and ensures clarity when the compiler is distinguishing between numbers and identifiers. Starting identifiers with a letter or underscore maintains consistency and readability in the code .

Newline characters play a crucial role in Java's output statements by ensuring proper formatting of text. They allow output text to be distributed across multiple lines, thereby enhancing readability and usability. For example, adding a newline character results in a line break, which can separate blocks or units of output (e.g., separating headings from content). This formatting capability is particularly useful in console applications where cohesive data presentation impacts user experience .

You might also like