Assignments Flashcards
1
Q
Reverse a string using while loop
A
public class reverse { public static void main(String[] arg){ int num = 54321; int result; int final1 = 0;
while(num > 0){ result = num % 10; final1 = final1 * 10 + result; num = num / 10; } System.out.println(final1); } }
2
Q
Given an array of integers, find the
sum of all the elements in the array.
A
class myApp{ public static void main(String[] args) {
int[] array = {1,2,3,4,5}; int sum = 0; for(int i = 0; i < array.length ; i ++ ){ sum = sum + array[i]; } System.out.println("The sum of arrays "+sum); } }
3
Q
Arrays.equals()
A
Compare two array by comparing each elements of the array.
import java.util.Arrays; public class MyClass { public static void main(String[] args) { int[] arr1 = {1, 2, 3, 4, 5, 6}; int[] arr2 = {1, 2, 3, 4, 5, 6};
if (Arrays.equals(arr1, arr2)) { System.out.println("Arrays are equal"); } else { System.out.println("Arrays are not equal"); } } }
4
Q
Create a class named Employee with: variables: emp no and emp name method: printEmployee which displays the employee information
A
class Employee{ //fields int emp_no; String emp_name;
//method void printEmployee(int no, String name){ emp_no = no; emp_name = name; } void display(){ System.out.println(emp_name+" "+emp_no); } }
public class MyClass{ public static void main(String[] args) { Employee e1 = new Employee(); e1.printEmployee(111,"Aryan"); e1.display();
} }