Q1: Find the output of the following program
public class Test4{
public static void main( String args[] )
{
System.out.print( “t*****n” );
System.out.println( “\” );
System.out.println( “”Java Escape sequences”");
System.out.println(“\” );
System.out.print(“t*****” );
System.out.println(); }}
Output:
*****
“Java Escape sequences”
*****
Q2: (Homework)
Write a java application that inputs three integers (use a Scanner object) from the user and displays the sum, average, product, smallest and largest of the numbers
Answer:
/*
* this program can take 3 inputs (integers) and he make the sum,product and
average, in addition he show you about smallest and largest number you have
had enter.
* I build this program by importing a java class (scanner) to get the inputs.
* I put the inputs inside an array.
*/
package ihkq2;
import java.util.Scanner; //import th scanner class
public class IHKq2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
//Initialisation and declaretion of the variables
int sum = 0;
int product = 1;
double average=0; //Using double for Decimal number
int largest = 0;
int smallest = 0;
int[] values = new int[3]; // Array
int PassedNum = 0;
//While loop for entering the value inside the array (values)
while (PassedNum < values.length)
{
System.out.println("Enter Number");
values[PassedNum] = scan.nextInt();
PassedNum++;
}
System.out.println("your values are " + values[0]+"," // Your inputes
+ values[1]+","+ values[2]);
//Using the for loop for calculation the sum and product
for(int i=0;i < values.length;i++)
{
sum += values[i];
product *= values[i];
}
//Calculation the average
average = (double) sum / values.length;
/*Here the logic how to calculate the smallest and largest number
by using the for loop*/
smallest = values[0];
largest = values[0];
for(int i = 0; i < values.length; i++)
{
if(values[i] < smallest)
smallest = values[i];
if(values[i] > largest)
largest = values[i];
}
//Here the output of the program
System.out.println(“Sum: ” + sum);
System.out.println(“Product: ” + product);
System.out.println(“averege: ” + average);
System.out.println(“smallest: ” + smallest);
System.out.println(“Largest: ” + largest);
}
}
Q3: Which of the followings are valid identifiers in Java? Select all that apply.
A. _3_
B. $variable$
C. this
D. thisvariable
E. 3temp
F. !test
G. Extends
Q4: what is the correct way of representing a char literal?
A. “c”
B. ‘c’
C. ‘000c’
D. ‘41234’
Q5: Which of the following are correct signatures for the main method? Select all that apply.
A. static public int main( String args[] )
B. public static void main( String [] args )
C. static public void main( String [] args )
D. static public void main( string a[] )
E. public static void main( String a[] )
Q6: Consider the following code segment:
Boolean flag = false;
if( flag == true )
System.out.println( “true” );
else
System.out.println( “false” );
A. It will print false;
B. it will print true;
C. It will print none.
D. Compilation error.
E. Runtime error.
Q7: What results from attempting to compile and run the following code?
public class Conditional{
public static void main( String args[] )
{
int x = 4;
System.out.println( “Value is” + ( ( x > 4 ) ? 99.9 : 9 ));
}
}
A. The output: value is 99.9
B. The output: value is 9
C. The output: value is 9.0
D. A compiler error at Line 5.
Q8: What is the output of the following program? Assume the user enters 12 for one execution of the program and 15 for a second execution?
import javax.swing.JOptionPane;
public class Compares{
public static void main( String a[] )
{
int integer;
String input;
input = JOptionPane.showInputDialog( “Enter an integer:” );
integer = Integer.parseInt( input );
if( (integer % 6 ) == 0 )
System.out.println( “Hello” );
else
System.out.println( “Good Bye” );
System.exit( 0 );
} }
Answer for Q8:
If the user enter 12 or (x*6) the output will be HELLO
If the user enter 15 or ! (x*6) the output will be GOOD BYE
Q9 What would happen when the following is compiled and executed. Select the one correct answer.
1. public class Compare {
2. public static void main(String args[]) {
3. int x = 10, y;
4. if(x < 10)
5. y = 1;
6. if(x>= 10) y = 2;
7. System.out.println(“y is ” + y);
8. }
9. }
10.
A. The program compiles and prints y is 0 when executed.
B. The program compiles and prints y is 1 when executed.
C. The program compiles and prints y is 2 when executed.
D. The program does not compile complaining about y not being initialized.
E. The program throws a runtime exception.
Q10: Write an application that prompts the user to enter a circle radius and calculates its diameter, area and circumference. The program should allow the user to choose from a menu.
Answer:
/*
* This application prompts the user to enter a circle radius and calculates
his diameter, area and circumference.
*
*/
package ihkq10;
import java.util.Scanner;
public class IHKq10 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println(“Please Enter the radius of ” +
“the circle and choose the option you want (-1 to exit)”);
int radius = scan.nextInt();
while(radius!=-1){ //Using the while loop here for multi running
System.out.println(“choosing option:n” + //Choosing the option
“1 for diametern” +
“2 for arean” +
“3 for circumference”);
int option = scan.nextInt();
if (option == 1){ //Option 1
double diameter =(2*radius);
System.out.println(“The Diameter of the cercle is ” + diameter);
}
if (option==2){ //Option 2
double pi=3.14;
System.out.println(“The area of the cercle is ” +((radius*radius)*pi));
}
if (option == 3){ //Option 3
System.out.println(“The circumference” +
” of the cercle is ” +((2*radius)*3.14));
}
if (option >3 || option<0) //Option not exist
System.out.println("Invalide option");
System.out.println("Please Enter the radius of " +
"the circle and choose the option you want (-1 to exit)");
radius = scan.nextInt();
}
}
}
Q11: Write an application that uses a scanner object to input an integer N (>2), then the program should display a square and an empty square out of stars of dimensions N on the cmd.
Answer:
/*
* this application uses a scanner object to input an integer N (>2),
the program display empty square out of stars of dimensions N on the cmd.
*/
package ihkq11;
import java.util.Scanner;
public class IHKq11 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int side=0, rowPosition, size;
System.out.println( “Enter the square side: “);
side = scan.nextInt();
size = side;
while ( side > 0 ) {
rowPosition = size;
while ( rowPosition > 0 ) {
if ( size == side || side == 1 || rowPosition == 1 ||
rowPosition == size )
System.out.print( “*”);
else
System.out.print( ” “);
–rowPosition;
}
System.out.print( “n”);
–side;
}
System.out.println();
}
}
Q12: Write an application that prompts the user to enter 5-digit integer ( Use a Scanner object). Separate the number into its individual digits, and then display the following:
a- The sum of the five digits being extracted.
b- The number of digits that are even.
c- The number of digits that are odd.
d- The number of digits that are zero.
Example: For the input 10234, the output will be:
Sum of digits: 1 + 0 + 2 + 3 + 4 = 10.
Zero digits count: 1.
Odd digits count: 2.
Even digits count: 2.
(Modify the code so it works for a number of N digits.)
Answer:
/*
* This application prompt the user to input 5-digit integer .
* This application will divide the integer of 5-digits and make the sum of
his digits, count the odd and even digits.
*
* and open the template in the editor.
*/
package ihkq12;
import java.util.Scanner;
public class IHKq12 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int number, first, second; //Initialization and declaration for the digits
int third, fourth, fifth; //Initialization and declaration
System.out.println(“Enter a five-digit number: “);
number= scan.nextInt();
first = number / 10000; //This will be the first digit
second = number % 10000 / 1000; //This will be the second digit
third = number % 10000 % 1000 / 100; //This will be the third digit
fourth = number % 10000 % 1000 % 100 / 10; //This will be the fourth digit
fifth = number % 10000 % 1000 % 10; //This will be the fifth digit
System.out.println(“You input is: “+first+” ”
+ second+” “+third+” “+fourth+” “+fifth );
int sum=0;
int[] values = {first, second, third, fourth, fifth};
int count=0;
int even=0;
int odd=0;
for(int i=0 ; i < values.length ; i++)
{
sum += values[i]; // Sum of all the elements of the array
if(values[i]==0)
count++;
if(values[i]%2==0) //The counter of the even numbers
even++;
if(values[i]%2==1) //The counter of the odd numbers
odd++;
}
if(count != 0){ //without this condition the “0″ will be even
even = even-count;
}
System.out.println(“Sum of digits ” + sum);
System.out.println(“Zero digits count ” + count);
System.out.println(“Odd digits count ” + odd);
System.out.println(“Even digits count ” + even);
}
}
This document Solved and uploaded by BOB for more info Infinitybob@hotmail.com