JAVA PRACTICE EXAM
Know that how good you are in programming
Problem A [Two’s Power]
Write a program that reads an integer n
and checks whether n is a power of 2 (powers of 2 are 1,2,4,8,16,32, …). The
output of your program is either “yes” or “no”.
public class LAU1 |
Problem B [No Duplicates]
Write a program that read a string str
and prints a string obtained from str by removing/ignoring duplicates. For
example, if str is abbacdcae, then your program prints abcde. In other words,
only the first occurrence of a character is printed.
import java.util.Scanner;
|
Problem C [Integer Mirror]
Write a program that reads an integer N followed
by N positive integers. For each entered integer, your program prints (on a
new line) its digits in reverse. For example:
Input
3
35423
7
98710789
Output
32453
7
98701789
import java.util.Scanner;
|
Problem D [Prime Frequency]
Write a program that reads positive
integers from the keyboard until the user enters 0. Then the program prints the
number of prime numbers entered. Here is a sample:
Input Output
2 3 5 0 3 |
public class LAU4
|
Problem E [Palindromes]
Write a program that reads an integer
N followed by N strings. For each string, your program prints yes or no
according whether the string is a palindrome or not. Here is a sample:
3
kayak yes
palindrome no
wow yes
Input Output
public class LAU5
|
Problem F [Flipped Star Triangles]
Complete the following program by
writing the method starTriangle that takes an integer i and draws a flipped triangle
(as shown below) of height i that is filled with stars. The main method (below)
calls starTriangle N times, each time with one of the numbers between 1 and N. For
example, if N is 3, the program calls starTriangle 3 times and prints:
*****
***
*
public static void main(String[] args) {
Scanner
input = new Scanner(system.in);
int N = input.nextInt();
for(int i = 1; i <= N; i++){
starTriangle(i);
System.out.println();
}
}
Problem G [Fibonacci Number]
Complete and submit the following
program by writing the boolean method isFib, which takes a positive integer n
and checks whether n is one of the Fibonacci numbers (1,1,2,3,5,8,13,…).
{
public class FibNum
|
public static boolean isFib(int n)
{
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n, testnums = input.nextInt();
for(int j = 1; j <= testnums; j++)
{
n = input.nextInt();
if(isFib(n))
System.out.println(“Yes”);
else System.out.println(“No”);
}
}
}
import java.util.Scanner; |
Problem H [Longest Natural
Successors]
Two consecutive integers are natural
successors if the second is the successor of the first in the sequence of
natural numbers (1 and 2 are natural successors). Write a program that reads a
number N followed by N integers, and then prints the length of the longest
sequence of consecutive natural successors. Example:
Input Output
7 2 3 5 6 7
9 10 3
import java.util.Scanner; |
No comments :
Post a Comment