0% found this document useful (0 votes)
9 views

Lect of COMP

The document contains examples of Java code snippets and exercises. It includes code to demonstrate type casting, string assignment, output from code fragments, errors in code examples, and exercises to generate random numbers, perform math operations, and manipulate strings.

Uploaded by

headcodfree
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 views

Lect of COMP

The document contains examples of Java code snippets and exercises. It includes code to demonstrate type casting, string assignment, output from code fragments, errors in code examples, and exercises to generate random numbers, perform math operations, and manipulate strings.

Uploaded by

headcodfree
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
You are on page 1/ 8

A) Java Exercises:

Exercise (1):

Type casting - convert the following double type (myDouble) to an int type:

double myDouble = 9.78d;


int myInt = myDouble;

Exercise (2):

Fill in the missing part to create a greeting variable of type String and assign it the
value Hello.

greeting = ;

B) What is the output of the following fragments:

1)
public class Quiz1 {
public static void main(String[] args) {
int a = 7;
int b = 3;
int c = ++a/--b;
System.out.println( c != 4);
System.out.println( a > c);
a += 2;
b *= 2;
System.out.println("a= "+ a );
System.out.println("b= "+ b);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
2)
public class Test {
public static void main(String[] args) {
char ch1 = 97;
char ch2 = 'A';
System.out.println(ch1==ch2 && ch1=='a');
System.out.println(ch2);
double x = 5.9;
int y = (int)x;
System.out.println(!(y>x || y==5.9));
System.out.println(y);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
3)
public class Test {
public static void main(String[] args) {
String txt1 = "JAVAS\b Programming \nis\\FUN, \"so\tfun\"";
String txt2 = "C# is Fun\rJava";
String a = "50";
int b = 50;
String c = a + b;
System.out.println(txt2);
System.out.println(txt1);
System.out.println("50 + 50 = 100\nc= " + c);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

C) Find the errors and correct them (only two errors):


1)
public class ComputeSalary {
public static void main(String[] args) {
final double RATE = 0.05;
int baseSalary = 1000.5;
double salary = baseSalary + (Rate * 100);
System.out.println("The Salary = " + salary);
}
}
………………………………………………………………………………………………
……………………………………………………………………………………………

D) Write a Java program:


1)

Write a complete java program that takes three numbers (x, y, z) as input
and print the output of (x+y)z and xy + yz
Test Data:
Enter first number: 5
Enter second number: 6
Enter third number: 7

Expected Output:
Result of specified numbers 5, 6, 7:

(x+y)z is 77

xy + yz is 72
Q1. What are the outputs of the following pieces of codes:

1.
public static void main(String[] args){
int r;
r=3;
System.out.println(" r="+r);
r=3;
r= r + r *2 + -1;
System.out.println(" r="+r);
}
}

2. public class First {


public static void main(String[] args){
int w = 12;
int b = 4;
int c = 5;
double y = 9;
double d = y / b;
int r = w % 2 + 4 / 3 + 2 * 3;
System.out.println(" d=" + d + " r=" + r);
}
}
3.
public class Test {
public static void main(String[] args) {
double myDouble = 15.8d;
int myInt = (int) myDouble;
System.out.println(myDouble);
System.out.println(myInt);
}
}
4.
public class First {
public static void main(String[] args){
System.out.println(" University of \"Tabuk\"");
System.out.println(" \\FCIT\\ \t Welcomes you at the \'Tabuk\'");
System.out.println(" This is our \"first C# Quiz\bzess !!! \" ");
}
}

5.
int x=5, y=2;
System.out.println ("x - y = " + (x – y));
6.
int x=1, y=2;
int w = ++x + y--;
System.out.println(x +" " + y + " "+w);

What is the error in each of the followings:


1.
public static void main(String[] args){
int a=25;
a == a +10;
System.out.println(a);
}

2.
int x= "10";
char y ="A";
string w = 'Ali'
Java Exercises:

A) What is the output of the following fragments:


1)
public class Test {
public static void main(String[] args) {
double a = Math.abs(-5.5);
double b = Math.pow(2, 2);
double max = Math.max(a, b);
double min = Math.min(a, b);
double c = Math.sqrt(b);
System.out.println("a= "+a+" b= "+b+" c ="+c);
System.out.println("Max= "+max+" Min= "+min);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
2)
public class Test {
public static void main(String[] args) {
String txt = "JAVA Programming is FUN, so fun";
System.out.println("The length of the txt string is: " + txt.length());
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
System.out.println(txt.indexOf("fun"));
System.out.println(txt.indexOf("java"));
System.out.println("Welcome ".concat("to java"));
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
3) Evaluate the following method calls:
(a) Math.sqrt(4.0) …………………………………………………………………
(b) Math.pow(2.0, 2.0) ……………………………………………………………
(c) Math.max(2.0, Math.min(3.0, 4.0)) …………………………………………...

4)
public class Example {
public static void main(String[] args) {
char i = 30 + 35;
System.out.println("i is " + i);
int j = 2 + 'a';
System.out.println("j is " + j);
System.out.println(j + " is the Unicode for character " + (char)j);
System.out.println("Chapter " + '9');
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
5)
public class Example {
public static void main(String[] args) {
String s1 = "Welcome to Java";
String s2 = " Programming is fun";
String s3 = "Welcome to Java";
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s1.indexOf('j'));
System.out.println(s1.indexOf("to"));
System.out.println(s1.length());
System.out.println(s1.toLowerCase());
System.out.println(s1.toUpperCase());
System.out.println(s1.concat(s2));
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

B) Write a Java program:


1) Write a Java program that generates a random number in the range of 0
through 49 (0 to 49)
2) Write an expression that obtains a random integer between 34 and 55.
Write an expression that obtains a random integer between 0 and 999. Write
an expression that obtains a random number between 5.5 and 55.5

You might also like