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

Practical 01 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practical 01 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

OOP Using Java- Practical 01

1. Write you first java programme to display “Hello World” on the screen.

2. Write a programme to display your name on the first line and to display your degree programme on
the second line on the screen. Please use command line (cmd) to execute your code.

3. Write down a programme to get the following output using a for loop. Repeat the same example by
using a while loop.

Executing Loop 0
Executing Loop 1
Executing Loop 2
Executing Loop 3
Executing Loop 4

4. Write a class and insert the following code block into the appropriate place. Execute the code and
get the result.

int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
if( x == 30 ){
break;
}
System.out.print( x );
System.out.print("\n");
}
System.out.print(“I’m out of the Loop now"); “

Results:

Repeat the same code using “continue” instead of “break”. Write down the output.

Results:

5. Write a class and insert the following code block into the appropriate place. Execute the code and
get the result.
1. char grade =‘A’;
2. switch(grade)
3. {
4. case 'A' :
5. System.out.println("Excellent!");
6. break;
7. case 'D' :
OOP Using Java- Practical 01

8. System.out.println("You passed");
9. case 'F' :
10. System.out.println("Better try again");
11. break;
12. default :
13. System.out.println("Invalid grade");
14. }
15. System.out.println("Your grade is " + grade);

Results:

Repeat the same removing “break” command at line number 6. Write down the output.

Repeat the same scenario by using if-else-if statement instead of switch case.

6. As of java 5 the enhanced for loop was introduced. This is mainly used for Arrays. Below code
contains few mistakes. First execute the code. Then identify the errors printed on the console.
Rectify all the errors and execute to get the output:

class TestEnhanceForLoop {

public static void mains(String args[]){

int [] numbers = {10, 20, 30, 40, 50};

for(int x : numbers ){

System.out.print( x );

System.out.print(",")

System.out.print("\n");

String [] names ={“James”, "Larry", "Tom", "Lacy"}

for( String name : names ) {

System.out.print( name );

System.out.print(",");

Output:

You might also like