You are on page 1of 12

1; what do you mean by java is platform independence?

2 ; Java Pyramid 1 Example


This Java Pyramid example shows how to generate pyramid or triangle like
given below using for loop.

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

public class JavaPyramid1 {

public static void main(String[] args) {

for(int i=1; i<= 5 ;i++){

for(int j=0; j < i; j++){


System.out.print("*");
}

//generate a new line


System.out.println("");
}
}

Output of the above program would be


*
**
***
****
*****
3; Java Pyramid 2 Example
This Java Pyramid example shows how to generate pyramid or triangle like
given below using for loop.

*****
****
***
**
*
public class JavaPyramid2 {

public static void main(String[] args) {

for(int i=5; i>0 ;i--){

for(int j=0; j < i; j++){

System.out.print("*");
}

//generate a new line


System.out.println("");
}
}
}
Output of the example would be
*****
****
***
**
*

4;

Java Pyramid 3 Example


This Java Pyramid example shows how to generate pyramid or triangle like
given below using for loop.

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

public class JavaPyramid3 {

public static void main(String[] args) {

for(int i=1; i<= 5 ;i++){

for(int j=0; j < i; j++){


System.out.print("*");
}

//generate a new line


System.out.println("");
}

//create second half of pyramid


for(int i=5; i>0 ;i--){

for(int j=0; j < i; j++){


System.out.print("*");
}

//generate a new line


System.out.println("");
}

}
}

Output of the example would be

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

5; Java Pyramid 5 Example


This Java Pyramid example shows how to generate pyramid or triangle like
given below using for loop.

12345
1234
123
12
1

public class JavaPyramid5 {

public static void main(String[] args) {

for(int i=5; i>0 ;i--){

for(int j=0; j < i; j++){


System.out.print(j+1);
}

System.out.println("");
}

}
}
Output of the example would be
12345
1234
123
12
1

6; This Java Pyramid example shows how to generate pyramid or triangle like
given below using for loop.

1
12
123
1234
12345

public class JavaPyramid4 {

public static void main(String[] args) {

for(int i=1; i<= 5 ;i++){

for(int j=0; j < i; j++){


System.out.print(j+1);
}

System.out.println("");
}

}
}

Output of the example would be


1
12
123
1234
12345

7; Write a program that inputs three integers from the user and displays the sum, average,
product, smallest and largest of the numbers.
8; Write a program that reads five integers and prints the largest and smallest integers in the
group.
9; write a program that prints A for exam grades greater than or equal to 90, B
for grades in the range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60
to 69 and F for all other grades:

10; For each of the following Java identifiers, note whether it is legal or illegal:
a. weeklySales
b. last character
c. class
d. public
e. myfirstinitial
f. phone#

g. abcdefghijklmnop
h. 23jordan
i. my_code
j. 90210
k. year2010budget
l. abffraternity

11; Identify and fix the errors in the following code:


public class Welcome {
public void Main(String[] args) {
System.out.println('Welcome to Java!);
}
)
12; Write a program that reads in the radius and length of a cylinder and computes volume using
the following formulas:
Area = radius * radius *
Volume = area * length

13; writes a program that reads and calculates the sum of an unspecified number
of integers. The input 0 signifies the end of the input (the input ends if it is 0)
By using
A; while loop
B; do while loop
14; what are the differences between a while loop and a do-while loop? Convert the
following while loop into a do-while loop.
Public class Converting {
Public static void main( string [] args){
Scanner input = new Scanner(System.in);
int sum = 0;
System.out.println("Enter an integer " +
"(the input ends if it is 0)");
int number = input.nextInt();
while (number != 0) {
sum += number;
System.out.println("Enter an integer " +"(the input ends if it is 0)");
number = input.nextInt();
}
}
}

15; Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, sum = 0, count;
for (count = 0; count < 5; count++) {
number = input.nextInt();
sum += number;
}
System.out.println("sum is " + sum);
System.out.println("count is " + count);
}
}
16; Which of the following statements are valid array declarations?
int i = new int(30);
double d[] = new double[30];
char[] r = new char(1..30);
int i[] = (3, 4, 3, 2);
float f[] = {2.3, 4.5, 6.6};
char[] c = new char();
17;
Indicate true or false for the following statements:
Every element in an array has the same type.
The array size is fixed after it is declared.
The array size is fixed after it is created.
The elements in an array must be of primitive data type.

18; Write statements to do the following:


a. Create an array to hold 10 double values.
b. Assign value 5.5 to the last element in the array.

c. Display the sum of the first two elements.


d. Write a loop that computes the sum of all elements in the array.
e. Write a loop that finds the minimum element in the array.
19; write a program to find the sum of two matrixes
20; write a program to display a multiplication table
(Use the following table as sample)
Multiplication table
1 2 3 4 5 6 7 8 9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12

You might also like