Java Basic Questions
Java Basic Questions
Semester 2020-2021
ITA5004 – Object Oriented Programming using JAVA
Programming Questions (Basic)
import java.util.*
public class factoria
}
}
fi
)
p=p*i
import java.util.*
public class bbonacc
System.out.print(a+",")
if(2<=n
System.out.print(b+",")
for(int i=3;i<=n;i++
}
}
fi
;
fi
;
int c=a+b
System.out.print(c+",")
a=b
b=c
else if(d==0
System.out.print("The roots are real and equal and there values are: ");
System.out.print((-b)/(2*a)+" & "+(b)/(2*a))
{
fi
;
fi
els
}
Q5.Write a Java program to get n numbers from the user and display th
number of positive, negative and zeroes.
Solution
import java.util.*
public class count_positive_negative_zer
a[i]=in.nextInt();
for(int i=0;i<n;i++
if(a[i]==0
ctr1++
else if(a[i]<0
{
ctr2++
els
ctr3++
}
Q6.Write a Java program to nd the sum of the series:1+2/2!+3/3
+…….n/n!
Solution
import java.util.*
public class series
int p=1
for(int j=1;j<=i;j++
p=p*j
}
fi
)
sum=sum+((double)i/(double)p)
import java.util.*
public class series
int p=1
for(int j=1;j<=i;j++
p=p*j
if(i%2==1
sum=sum+(double)(Math.pow(x,i)/p)
}
fi
;
els
sum=sum-(double)(Math.pow(x,i)/p)
Q8. Write a Java program to nd the given number is Armstrong or not. (if
the sum of cubes of the digits of the number is equal to the given number)
Solution:
import java.util.*
public class armston
int d=x%10
s=s+d*d*d
x=x/10
if(s==n
System.out.println("Armstong number")
els
}
fi
)
Q9. Write a program that accepts three numbers from the user and prints
"increasing" if the numbers are in increasing order, "decreasing" if the
numbers are in decreasing order, and "Neither increasing or decreasing
order" otherwise.
Solution:
import java.util.*
public class increase_decrease_neithe
System.out.println("Increasing")
System.out.println("Decreasing")
els
Solution:
import java.util.*
public class prime_factor
{
public static void main(String args[]
if(n%i==0
k=obj.prime(i)
if(k!=0
System.out.print(k+",")
int ctr=0,p=0
for(int i=1;i<=x;i++
if(x%i==0
ctr++;
if(ctr==2
p=x
return p
import java.util.*
public class pattern
for(int i=1;i<=n;i++
for(int j=1;j<=i;j++
System.out.print(i)
System.out.println()
import java.util.*
public class pattern
for(int j=i;j>=1;j--
System.out.print(j)
}
System.out.println()
Q13. Write a Java program to print the k-th digit from last. e.g. input
23617 and k=4 output 3.
Solution:
import java.util.*
public class kth_digi
d=n%10
n=n/10
i++
Q14. Write a Java program that reads two numbers (assume that both
have the same number of digits). The program outputs the sum of the
product of corresponding digits.
Input: 327 and 539
output : 3*5+2*3+7*9=84.
Solution:
import java.util.*
public class sum_product_digit
int d=a%10
int d1=b%10
s=s+d*d1
a=a/10
b=b/10
}
{
import java.util.*
public class count_digit
int d=x%10
c++
x=x/10
fi
)
Q16.Write a Java program to read input values from the user continuously.
The program should terminate if exactly three String values have been
inputted. Display the count of integer values and oat values entered so
far. Also, display the average of all integer values and all oat values
individually.
Solution:
fl
fl