ICM104 Kavinda Peiris - Java Literals - Assignment Week 02
ICM104 Kavinda Peiris - Java Literals - Assignment Week 02
Java Literals
15th July 2023
1. class Example{
public static void main(String []args){
System.out.println("Kavinda Peiris");
}
}
Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q1
Kavinda Peiris
2. class Q2{
public static void main(String []args){
System.out.println("Kavinda Peiris");
System.out.println("No:122, Senanayake Road, Bandarawela");
System.out.println("0773279280");
System.out.println("[email protected]");
}
}
Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q2
Kavinda Peiris
No:122, Senanayake Road, Bandarawela
0773279280
[email protected]
3. class Q3{
public static void main(String []args){
System.out.println("*");
System.out.println("* *");
System.out.println("* * *");
System.out.println("* * * *");
}
}
Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q3
*
**
***
****
4. class Q4{
public static void main(String []args){
System.out.println(" 299");
System.out.println("+ 800");
System.out.println("-------------");
System.out.println(" 1099");
1|Page iCM104 – Kavinda Peiris
System.out.println("=============");
}
}
Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q4
299
+ 800
-------------
1099
=============
5. class Q5{
public static void main(String []args){
System.out.println("* * * * * * =======================================");
System.out.println(" * * * * * =======================================");
System.out.println("* * * * * * =======================================");
System.out.println(" * * * * * =======================================");
System.out.println("* * * * * * =======================================");
System.out.println(" * * * * * =======================================");
System.out.println("* * * * * * =======================================");
System.out.println(" * * * * * =======================================");
System.out.println("* * * * * * =======================================");
System.out.println("===================================================");
System.out.println("===================================================");
System.out.println("===================================================");
System.out.println("===================================================");
System.out.println("===================================================");
System.out.println("===================================================");
}
}
Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q5
* * * * * * =======================================
* * * * * =======================================
* * * * * * =======================================
* * * * * =======================================
* * * * * * =======================================
* * * * * =======================================
* * * * * * =======================================
* * * * * =======================================
* * * * * * =======================================
===================================================
===================================================
===================================================
===================================================
===================================================
===================================================
6. Program A
H:\ICM\Assignments\Day 2\Java Literals>java Example
Institute of Computer Engineering Technology
223 A,
2|Page iCM104 – Kavinda Peiris
Galle Road,
Panadura.
Program B
H:\ICM\Assignments\Day 2\Java Literals>java Example
Institute of Computer Engineering Technology
223 A,Galle Road,Panadura.
7. If we want cursor to go to the next line after displaying something we can use “println”. But if we don’t want to
go to the next line after displaying something we can use “print”.
In this code first display ‘Hello’ and then go to the next line and then display ‘World’ with ‘!’ as a single world
like ‘World!’.
8. In line 1, “a” is string type and the second line ‘a’ is a character type.
10. Types of java literals are integer, floating-point, string, character, Boolean and null literals.
Example of literals
class JavaLiterals{
public static void main(String []args){
int x=8;
double y=8.0;
char z='z';
String a="Kavinda";
boolean bool = true;
}
}
11. Line 1 display normal number, second line display decimal value of the given binary value, third line display octal
value and last line display hexadecimal value of the given value separate lines.
EF
G
H
15. Output –
Hello
JAVA
Hello JAVA
HelloJAVA
\Hollo JAVA\
“Hello
JAVA”
‘Hello
JAVA’
Escape Sequences
\n – cursor got to the next line
\t – make tab space
\b – remove single space or letter before ‘\b’ that. Ex “Hello \bWorld” prints like a “HelloWorld”.
\\ - display back slash
\” – display double quotes
\’ – display single quotes
23. Output-
10
20
30
200
24. Output –
100
200
100
300
Explanation – First declare two integer type variable x and y. Then value 100 assign to x and value 200 assign to
y. After that Print x and y. Then again value 300 assign to y. So now value of y is 300. Then again print x and y.
25. B is the legal answer. A is illegal because there has no any value assign to x. C and D are also illegal because in
code x is declared as integer type variable. So it’s not allow declare same variable two times. E is also incorrect
because if we insert nothing it shows error because of x has no any value.
26. D. Compile error at line 6. Because in line 6 it’s not allow “int i=j=k=10” in java. It’s valid like this “int i,j,k=10”.
27. Output –
1020
30
Explanation – In line 1, two strings concatenate. In line 2, value 20 added to the value 10 and display the result.
28. Output –
In this code ‘pubic’ keyword missing ‘l’. So it display error message only. But if “pubic” replace with
“public” it’s output like this.
60
10+20+30
10+2030
102030
1050
3030
102030
30. In question 20 value 103 assigned to the integer type x variable. But in here some value is assigned by the user
input. But this code has missing one line “Scanner input = new Scanner(System.in);”
37. A. l = 1147383947;
C. l = 0xbacc;
D. l = 04032;
E. l = 0101110110L;
All are legal.
38. new, My Variable, @name, user-input, main, $percent, 123num, java.org are not valid identifiers.
39. Output:
32767
-32768
---------------------------------------------------------------------
Double
class Example{
public static void main(String args[]){
double x;
x=Double.MAX_VALUE;
System.out.println(x);
x=Double.MIN_VALUE;
System.out.println(x);
}
}
--------------------------------------------------------------------
Int
class Example{
public static void main(String args[]){
int x;
x=Integer.MAX_VALUE;
System.out.println(x);
x=Integer.MIN_VALUE;
System.out.println(x);
}
}
40. Output:
A
66
A1
a
98
a1
43. “char a = '\u0061';” and “char \u0061 = 'a';” and “ch\u0061r a = 'a';” are correct answers.
44. Output:
true
true
true
true
true
true
true
explanation : In first declare a character type a variable and assign ‘a’ as the value. Then compare a with ‘\u0061’
value. This is true because both are equals. That’s why it display true. Other lines works same as the first one.
45. In line 1 display some text inside the double quotes and line 2 display the value of string type s.
47. Output:
z is: 131y105z
scanner.nextLine();
//output part
System.out.println(f_name+" is my best friend. He is "+age+" old and lives in the beautiful town
of "+place+".");
}
}
//enter marks
System.out.print(sub1+" marks - ");
int mark1 = scanner.nextInt();
//enter marks
System.out.print(sub2+" marks - ");
int mark2 = scanner.nextInt();
//enter marks
System.out.print(sub3+" marks - ");
int mark3 = scanner.nextInt();
//total calculation
int tot = mark1 + mark2 + mark3;
//output part
System.out.println("\n\n"+sub1+"\t\t"+mark1);
System.out.println(sub2+"\t\t"+mark2);
System.out.println(sub3+"\t"+mark3);
System.out.println("\nTotal\t\t"+tot);
}
}
System.out.println(num1+" "+num2);