Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Wednesday, February 18, 2015
Java program to display multiplication table of any number

class Table
{
public static void main(String...s)
{
int n,i;
n=Integer.parseInt(s[0]);
for(i=1;i<=10;++i)
System.out.println(n+" * "+i+" = "+(n*i));
}
}
path classpath java home jre home settings for pentaho community edition in windows 64 bit OS
* set as it is in the image(User variables) do not concentrate on system variables
CLASSPATH,JAVA_HOME,PATH

System variables
JAVA_HOME, JRE_HOME for pentaho community installation.

Thanks,
Sadakar
Monday, February 16, 2015
Java Program to Find Union of two Arrays
For example we have two sorted arrays a1[]={2,3,5,11} and a2[]={4,7,9} then union of a1 and a2 will be {2,3,4,5,7,9,11}. A Java program for finding union of two arrays is given below.
Also Read: Java Program to Find Smallest and Largest Element in an Array
class demo
{
public static void main(String...s)
{
int i,j,n1,n2;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of elements of first array:");
n1=sc.nextInt();
System.out.print("Enter number of elements of second array:");
n2=sc.nextInt();
int a1[]=new int[n1];
int a2[]=new int[n2];
System.out.print("
Enter elements of first array in ascending order:");
for(i=0;i<n1;++i)
a1[i]=sc.nextInt();
System.out.print("
Enter elements of second array in ascending order:");
for(i=0;i<n2;++i)
a2[i]=sc.nextInt();
i=j=0;
System.out.print("
Union of Arrays: ");
while(i<n1&&j<n2)
{
if(a1[i]<a2[j])
{
System.out.print(a1[i]+" ");
i++;
}
else
if(a2[j]<a1[i])
{
System.out.print(a2[j]+" ");
j++;
}
else
{
System.out.print(a1[i]+" ");
i++;
j++;
}
}
if(i<n1)
while(i<n1)
{
System.out.print(a1[i]+" ");
i++;
}
if(j<n2)
while(j<n2)
{
System.out.print(a2[j]+" ");
j++;
}
}
}

Also Read: Java Program to Find Smallest and Largest Element in an Array
Java Program to Find Union of two Arrays
import java.util.Scanner; //import Scanner class in our programclass demo
{
public static void main(String...s)
{
int i,j,n1,n2;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of elements of first array:");
n1=sc.nextInt();
System.out.print("Enter number of elements of second array:");
n2=sc.nextInt();
int a1[]=new int[n1];
int a2[]=new int[n2];
System.out.print("
Enter elements of first array in ascending order:");
for(i=0;i<n1;++i)
a1[i]=sc.nextInt();
System.out.print("
Enter elements of second array in ascending order:");
for(i=0;i<n2;++i)
a2[i]=sc.nextInt();
i=j=0;
System.out.print("
Union of Arrays: ");
while(i<n1&&j<n2)
{
if(a1[i]<a2[j])
{
System.out.print(a1[i]+" ");
i++;
}
else
if(a2[j]<a1[i])
{
System.out.print(a2[j]+" ");
j++;
}
else
{
System.out.print(a1[i]+" ");
i++;
j++;
}
}
if(i<n1)
while(i<n1)
{
System.out.print(a1[i]+" ");
i++;
}
if(j<n2)
while(j<n2)
{
System.out.print(a2[j]+" ");
j++;
}
}
}

Wednesday, February 11, 2015
My First Java Program Print a Message Hello World

class HelloWorld
{
public static void main(String...s)
{
System.out.println("Hello World");
}
}
Tuesday, February 10, 2015
Java Interview Questions
Here I am providing the most important java interview questions that are frequently asked in interviews. If you want to submit any core java or advance java interview questions or if you find any mistake in below questions than please notify us by the contact us page.


Core Java Interview Questions
Q. Can we keep more than one class in a single java file?
A. Yes
Q. Can we keep main method in each class?
A. Yes
Q. Can we keep more than one main method in a single class?
A. Yes, with different prototype.
class A
{
public static void main(String...s)
{
System.out.println("Original main method");
}
public static void main(int s1)
{
System.out.println("Duplicate main method");
}
}
Q. Can we call main method explicitly?
A. Yes
class A
{
public static void main(String...s)
{
System.out.println("The Crazy Programmer");
}
}
class B
{
public static void main(String...s)
{
A.main();
}
}
Q. Can we execute our program without main method?
A. Yes, by using static block (supported till jdk 1.6).
class A
{
static
{
System.out.println("The Crazy Programmer");
}
}
Q. Why character takes 2 bytes in java?
A. Because java support UNICODE, and for supporting UNICODE minimum 2 bytes are required.
Q. Is java purely or truly object oriented language?
A. No, because it supports primitive data types.
Q. Does constructor return any value?
A. Yes, it returns current object reference (this).
Q. Constructor is called before or after object creation?
A. It is called at the time of creation of object.
Q. Constructor is static or non-static?
A. 90% constructor is static by nature.
Q. Can constructor overloaded?
A. Yes, constructor is special method and method can be overloaded.
Q. What is the use of default constructor?
A. To initialize the default value.
Q. Inheritance occurs at compile time or run time?
A. At runtime, it can be proved by simply decompiling the .class file of child class.
Q. What super holds?
A. It holds reference id of parent section of child class object.
Q. What is first line of constructor, this or super?
A. By default first line of constructor is super. Both super and this can’t be used together.
Q. Why we use dynamic binding?
A. To achieve abstraction.
Q. Can we keep constructor in abstract class?
A. Yes, each class in java have constructor.
Q. Can we make abstract method static?
A. No, because static is accessed via class name and it will be accessed by anyone.
Q. Why we make interface methods by default public and abstract?
A. Because methods are implemented by end user.
Q. How multiple inheritance is implemented in java?
A. Multiple inheritance is implemented by interface.
interface my1
{
. . . . .
. . . . .
}
interface my2
{
. . . . .
. . . . .
}
interface my3 extends m1,m2
{
. . . . .
. . . . .
}
Q. What is difference between String and StringBuffer?
A. In String object is immutable (can’t be modified) while in StringBuffer object is mutable (can be modified).
Subscribe to:
Posts (Atom)