Você está na página 1de 8

# LEBANESE INTERNATIONAL UNIVERSITY

Fall 2009-2010 HW1 This document Solved and uploaded by BOB for more info Infinitybob@hotmail.com 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. B. C. D. E. It will print false; it will print true; It will print none. Compilation error. 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.