Find the output of the following Programs:
1. public class Script1
{
public static void main(Object[]args){
int result = add(1, 2);
System.out.println(result);
}
int add(int x, int y)
{
return x+y;
}
double add(int x, int y)
{
return x+y;
}
}
2. public class Script2
{
public static void main(Object[]args){
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello"); //Using constructor
if(str1==str2)
Sysout("Equal 1");
else
Sysout("Not Equal 1");
if(str1 == str3)
Sysout("Equal 2");
else
Sysout("I am constructed using constructor")
if(str1.equals(str3))
Sysout("Equal 3")
else
Sysout("Not Equal 3")
3. interface IParent
{
void printValue();
}
interface Iparent_New extends IParent
{
void demoPrint();
}
public class DemoClass implements IParent_New
{
public static void Main(String[]args)
{
IParent_New parent_New=New DemoClass();
parent.New.demoPrint():
}
public void demoPrint()
{
Sysout("Inside demoPrint");
}
}
4. try{
try{
res=num/0;
Sysout("The result is" +res);
}
catch(ArithmeticException e)
{
Sysout("divided by zero");
throw new FileNotFoundException();
}
catch (FileNotFoundException e)
{
Sysout("File not found");
}}
catch(Exception e)
{
Sysout("Exception Found");
}
5. class Animal
{
String getColour()
{
return "Black";
}}
class Dog extends Animal
{
String getColour()
{
return "White";
}
}
public class Script2
{
public static void main(Object[]args)
{
Animal animal = new Dog();
Sysout(animal.getColour());
}
}
6. class Maps
{
public static void main(String[]args)
{
HashMap obj = new HashMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj);
}
}
7. Write a program to find wheather given no is Armstrong or not.
Example: Input - 153
Output - 1^3+5^3+3^3 = 153, so it is Armstrong no.
8. Write a program to Sort a given array without using library functions.
Array arr = {5,7,1,9,200,90,10,50,80}
9. Write a java program to count the number of words in a string.
String str = "You are given an array of numbers.Find out the array index or position.";
10. int count =0;
if(++count>0 && count++<2)
Sysout("Inside IF");
else
Sysout("Inside Else");
11. class evaluate{
public static void main(String[]args){
{
int arr[] = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n=6;
n = arr[arr[n] / 2];
Sysout(arr[n] / 2);
}}
1. public class Script1
{
public static void main(Object[]args){
int result = add(1, 2);
System.out.println(result);
}
int add(int x, int y)
{
return x+y;
}
double add(int x, int y)
{
return x+y;
}
}
2. public class Script2
{
public static void main(Object[]args){
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello"); //Using constructor
if(str1==str2)
Sysout("Equal 1");
else
Sysout("Not Equal 1");
if(str1 == str3)
Sysout("Equal 2");
else
Sysout("I am constructed using constructor")
if(str1.equals(str3))
Sysout("Equal 3")
else
Sysout("Not Equal 3")
3. interface IParent
{
void printValue();
}
interface Iparent_New extends IParent
{
void demoPrint();
}
public class DemoClass implements IParent_New
{
public static void Main(String[]args)
{
IParent_New parent_New=New DemoClass();
parent.New.demoPrint():
}
public void demoPrint()
{
Sysout("Inside demoPrint");
}
}
4. try{
try{
res=num/0;
Sysout("The result is" +res);
}
catch(ArithmeticException e)
{
Sysout("divided by zero");
throw new FileNotFoundException();
}
catch (FileNotFoundException e)
{
Sysout("File not found");
}}
catch(Exception e)
{
Sysout("Exception Found");
}
5. class Animal
{
String getColour()
{
return "Black";
}}
class Dog extends Animal
{
String getColour()
{
return "White";
}
}
public class Script2
{
public static void main(Object[]args)
{
Animal animal = new Dog();
Sysout(animal.getColour());
}
}
6. class Maps
{
public static void main(String[]args)
{
HashMap obj = new HashMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj);
}
}
7. Write a program to find wheather given no is Armstrong or not.
Example: Input - 153
Output - 1^3+5^3+3^3 = 153, so it is Armstrong no.
8. Write a program to Sort a given array without using library functions.
Array arr = {5,7,1,9,200,90,10,50,80}
9. Write a java program to count the number of words in a string.
String str = "You are given an array of numbers.Find out the array index or position.";
10. int count =0;
if(++count>0 && count++<2)
Sysout("Inside IF");
else
Sysout("Inside Else");
11. class evaluate{
public static void main(String[]args){
{
int arr[] = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n=6;
n = arr[arr[n] / 2];
Sysout(arr[n] / 2);
}}