Assignment 1 - Answers

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Assignment 1_ Answers

1.Convert below data types using Widening and Narrowing.


int a = 500;
long b = 7823;
double c = 34.5d;
float d = 78.3f;
Convert as follows
1. convert double c value to integer. What is this concept called? (Manual casting or Automatic casting)?
2. convert integer a value to double. What is this concept called? (Manual casting or Automatic casting)?
3. convert float d value to double. What is this concept called? (Manual casting or Automatic casting)?
4. convert long b value to a. What is this concept called? (Manual casting or Automatic casting)?

Answer –

int e = (int)c;
double f = (double)a;
double g = (double)d;
int h = (int)b;
2.Write code to print “Evotech” 5 times using FOR Loop, While Loop, and Do While loop
Expected output.

Answer –

(FOR Loop)
for (int i=1; i<=5; i++){
System.out.println("Evotech");
}
(While Loop)
int i = 1;
while(i<=5){
System.out.println("Evotech");
i++;
}
(Do While loop)
int i = 1;
do{System.out.println("Evotech");
i++;
}while(i<=5);

3. What is the different between While loop and do While loop?

The while loop in java executes one or more statements after testing the loop continuation condition at the
start of each iteration. The do-while loop, however, tests the loop continuation condition after the first
iteration has completed. Therefore, the do-while loop guarantees one execution of the loop logic whereas the
while does not.

4. Loop Following array using For loop and For each Loop

String[] Names={“Amal”, “Asanka”, “Nimali”, “Wimal”, “Akiff”}

Answer –
String [] Names = {"Amal","Asanka","Nimal","Wimal","Akiff"};
(For each Loop)
for (String i:Names){
System.out.println(i);

}
(For loop)
for (int i = 0; i < Names.length; i++) {
String name = Names[i];
System.out.println(name);
}
5.Write Java code to identify odd and even numbers and store in separate arrays (oddarr, evenarr).

int[] numbers={0,1,2,3,4,5,6,7,8,9,10,11}

int [] oddarr = new int[6];


int [] evenarr = new int[6];

Answer –

int [] numbers = {0,1,2,3,4,5,6,7,8,9,10,11};

int[] oddarr = new int[6];


int[] evenarr = new int[6];

int oddCount = 0;
int evenCount = 0;

for(int number:numbers){
if (number %2==0){
evenarr[evenCount] = number;
evenCount++;
}else{
oddarr[oddCount] = number;
oddCount++;
}
}

System.out.println("Even Numbers:");
for(int i=0;i<evenCount; i++){
System.out.println(evenarr[i]);
}
System.out.println("Odd Numbers");
for(int i=0;i<oddCount;i++){
System.out.println(oddarr[i]);
}

6. What are the following code output?


i) int mark =10;
String ans = (mark>=45)? “Pass”: “Failed”;

Answer – Failed

The code snippet you provided assigns the value 10 to the variable mark. It then uses the conditional
(ternary) operator (mark>=45)? "Pass”: "Failed" to determine the value of the ans variable.

In this case, since the value of mark is 10, which is less than 45, the condition (mark>=45) evaluates to
false. Therefore, the value assigned to ans will be "Failed".

ii)
int a = 10, b=20;
if((a!=b) || (b>=21)){
System.out.println(a);
}else if((b!=12) && (a>=9)){
System.out.println(b);
}

Answer – 20

1. int a = 10, b = 20; - Two integer variables a and b are declared and initialized with the values 10 and
20, respectively.
2. if ((a != b) || (b >= 21)) { ... } - This is the first conditional statement. Since (a != b) is true (10 is not
equal to 20), the first condition evaluates to true. The second condition (b >= 21) is false (20 is not
greater than or equal to 21), but since the first condition is true, the overall result is true. Therefore,
the code execution enters the if block.

3. System.out.println(a); - This statement is executed since the condition in the if block is true. It
prints the value of variable a, which is 10, to the console.
4. Since the if block is executed, the code does not enter the else if block, and the program terminates
after printing 10 as the output.

7. Take three numbers from the user and print the greatest number. (Use Scanner)

Example Input:

Input the 1st number: 25


Input the 2nd number: 78
Input the 3rd number: 87

Expected Output:
The greatest: 87

Answer –

package javaapplication_assignment_01;

import java.util.Scanner;

public class JavaApplication_Assignment_01 {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Input the 1st number: ");


int number1 = scanner.nextInt();

System.out.println("Input the 2nd number: ");


int number2 = scanner.nextInt();

System.out.println("Input the 3rd number: ");


int number3 = scanner.nextInt();
int greatestNumber = Math.max(number1, Math.max(number2, number3));
System.out.println("The greatest: " + greatestNumber);
}
}

8.Write a program in Java to display the multiplication table of a given integer.


Input
Input the number : Input number of terms : 5

Expected Output :

5X0=0
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25

Answer –

package javaapplication_assignment_01;

import java.util.Scanner;

public class JavaApplication_Assignment_01 {

Scanner input = new Scanner(System.in);

System.out.print("Input the number: ");


int number = input.nextInt();

System.out.print("Input number of terms: ");

int terms = input.nextInt();

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

int result = number * i;

System.out.println(number + " X " + i + " = " + result);

input.close();

9. Write a Java program that keeps a number from the user and generates an integer between 1 and 7 and
displays the name of the weekday. (Use Scanner and Switch Statement)
Test Data
Input number: 3

Expected Output:
Wednesday

Answer –

package javaapplication_assignment_01;

import java.util.Scanner;

public class JavaApplication_Assignment_01 {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = scanner.nextInt();

// Generate a random number between 1 and 7


int randomNum = (int) (Math.random() * 7) + 1;

switch (randomNum) {

case 1:

System.out.println("Sunday");

break;

case 2:

System.out.println("Monday");

break;

case 3:

System.out.println("Tuesday");

break;

case 4:

System.out.println("Wednesday");

break;

case 5:

System.out.println("Thursday");

break;

case 6:

System.out.println("Friday");

break;

case 7:

System.out.println("Saturday");

break;

default:

System.out.println("Invalid weekday");

break;

}
}

10. Write a program to print the following star pattern. (Use for loops)

A.

int rows = 5;

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

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

System.out.print("*");

System.out.println();

B.

int rows = 5;

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

for (int j = 2*(rows-i); j>=0; j--) {

System.out.print("*");

System.out.println();
}

You might also like