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

Elementary Programming - Examples

The document contains Java exercises and code snippets including: 1) Converting a double to an int and assigning a string variable. 2) Code snippets that output true/false and variable values. 3) A code snippet with string concatenation and formatting. 4) Code snippets with errors to be corrected around variable naming. 5) A program to take 3 user inputs and perform calculations on them, outputting the results.

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)
14 views

Elementary Programming - Examples

The document contains Java exercises and code snippets including: 1) Converting a double to an int and assigning a string variable. 2) Code snippets that output true/false and variable values. 3) A code snippet with string concatenation and formatting. 4) Code snippets with errors to be corrected around variable naming. 5) A program to take 3 user inputs and perform calculations on them, outputting the results.

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/ 3

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

You might also like