0% found this document useful (0 votes)
9 views4 pages

Java Programming Basics: Output Exercises

The document outlines a programming assignment consisting of various tasks to be completed in Java. These tasks include displaying messages, formatting output as a letter, printing initials in block letters, creating shapes with stars, and manipulating variables. Additionally, it includes questions about the output of specific Java statements and operations involving integer and double types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Java Programming Basics: Output Exercises

The document outlines a programming assignment consisting of various tasks to be completed in Java. These tasks include displaying messages, formatting output as a letter, printing initials in block letters, creating shapes with stars, and manipulating variables. Additionally, it includes questions about the output of specific Java statements and operations involving integer and double types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming Assignment-I

(Simple Output Statements)


1. Write a java program to display following messages.
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.

2. Write a java program that displays your name and address on the screen as if it
were a letter. Your output should look something like that below.
+-------------------------------------------------------------------------------------+
| #### |
| #### |
| #### |
|
| Biswanath Dash|
| H.N:109, Baramunda|
| Bhubaneswar|
+--------------------------------------------------------------------------------------+

Page 1 of 4
3. Write a java program to display your initials on the screen in block letters as
shown. For example the name Tapan Kumar
TTTTTTTTTT KK KK
TT KK KK
TT KK KK
TT KK
TT KK KK
TT KK KK
TT KK KK

4. Write a java program to print an equilateral triangle using star ' * '.
*
**
***
****
*****
5. Write a java program that stores your Regd. No and year of admission into two
variables, and displays their values on the screen.
My Regd. No is 1961020012 and I have taken admission in MCA In 2023.
6. Let we have two values 113, 2.71828. Then, declare two different variables to hold
the given values. Write the java program to display the values of these two variables
on the screen, one per line.
This is room # 113
e is close to 2.71828

Page 2 of 4
7. Write a java program to exchange the values of two variables of integer type A and
B using third temporary variable C.
8. Write a java program to exchange the values of two variables of integer type A and
B without using third temporary variable.
9. What do each of the following print?
a. [Link](2 + "bc");
b. [Link](2 + 3 + "bc");
c. [Link]((2+3) + "bc");
d. [Link]("bc" + (2+3));
e. [Link]("bc" + 2 + 3);

10. What do each of the following print?


a. [Link]('b');
b. [Link]('b' + 'c');
c. [Link]((char) ('a' + 4));

11. Suppose that a variable a is declared as int a = 2147483647


(or equivalently, Integer.MAX_VALUE ). What do each of the following print?
a. [Link](a);
b. [Link](a+1);
c. [Link](2-a);
d. [Link](-2-a);
e. [Link](2*a);

Page 3 of 4
f. [Link](4*a);

12. Suppose that a variable a is declared as double a = 3.14159. What do each of the
following print?
a. [Link](a);
b. [Link](a+1);
c. [Link](8/(int) a);
d. [Link](8/a);
e. [Link]((int) (8/a));

13. Assume a string variable ruler1 contains “1” initially i.e. String ruler1=”1” . Write
a java program to print the following output using string concatenation. (You can take
extra string variables)
1
121
1213121
121312141213121

Page 4 of 4

Common questions

Powered by AI

Using structured output formatting, such as displaying initials in block letters, aids in learning how to manipulate output for readability and visual design. It demonstrates how spatial alignment and repetition can visually represent patterns, enhancing understanding of data flow and user interface design in programming tasks. This skill is critical for creating user-friendly interfaces .

The expression System.out.println(a) directly prints the floating-point number stored in variable a up to the precision Java supports for double types, typically around 15 decimal places. Thus, the full precise value of 3.14159 is printed without alteration, unless further formatted or altered prior to printing. It showcases Java's handling of double precision .

System.out.println('b') prints the character 'b', whereas System.out.println('b' + 'c') sums the ASCII values of 'b' (98) and 'c' (99) and prints the integer result, 197. The '+' operator typically adds numbers, but when encountering characters, it converts them to their ASCII integer values .

When an integer variable a, set to Integer.MAX_VALUE, has 1 added to it, it overflows and wraps around to Integer.MIN_VALUE due to Java's fixed integer size and lack of overflow detection. This result highlights the need for careful handling or libraries for checking overflows in Java .

In System.out.println((char) ('a' + 4)), 'a' is converted to its ASCII value, 97, and 4 is added, resulting in 101. Casting this result back to a char yields 'e', the character corresponding to ASCII value 101. This demonstrates the use of arithmetic operations on character data and conversion between types in Java, highlighting Java’s flexibility but also risks if type conversions are not handled deliberately .

In Java, expression evaluation starts from left to right. In System.out.println(2 + 3 + "bc"), the addition operation is performed first, resulting in 5, which is then concatenated with 'bc', yielding "5bc". Conversely, System.out.println("bc" + 2 + 3) first treats 'bc' as a string, so 2 and 3 are concatenated as strings, resulting in "bc23". The dynamic type conversion in concatenation with strings leads to these differences .

Directly printing char literals like in System.out.println('b') outputs 'b', showing the literal handling of characters. On the other hand, in System.out.println((char) ('a' + 4)), the operation involves type conversion and arithmetic before outputting a character corresponding to a calculated ASCII value. Thus, while both yield character outputs, one processes through computation and conversion first, illustrating Java's capacity to handle characters both as literals and numeric codes .

Using a temporary variable allows the values of A and B to be swapped without losing any data. This is because the temporary variable stores one of the original values while the other is being moved into its new place, effectively acting as an intermediate holder. Without this, simple assignments would overwrite the original values, resulting in data loss .

String concatenation as exemplified by String ruler1 = "1"; and subsequent manipulations highlights Java's rich string handling capabilities. Concatenating further strings dynamically expands content, demonstrating mutability via operations and encouraging design in dynamic user interfaces or text processing. It also reveals performance considerations, as each concatenation can create new objects, impacting efficiency if heavily used .

In (8/(int)a), the double a is explicitly cast to an integer, truncating any decimal portion, resulting in 3. So the division computes as 8/3 with integer arithmetic, giving an integer output of 2. Whereas in 8/a, a remains a double, leading to floating-point division and yielding a precise result of approximately 2.54648. This distinction highlights how casting impacts the precision and type of the resultant operations in Java .

You might also like