CSCI 1301 Test III

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which of the following expression yields an integer between 0 and 100, inclusive?

(int)(Math.random() * 101)

Show the output of the following code: public class Test1 { public static void main(String[] args) { int i = 1; while (i < 10){ System.out.print(i + " "); i = i + 3; } } }

1 4 7

How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);

10

What is the value in count after the following loop is executed? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 9); System.out.print(count);

10

How many times will the following code print "Welcome to GHC"? int count = 0; do { System.out.println("Welcome to GHC"); } while (count++ < 10);

11

What is the output on the following fragment? int i = 1; int j = 1; while (i < 5 ) { i++; j = j*2; } System.out.println(j);

16

public class Test { public static void main( String[] args) { int i = 1; while (i <= 4) { int num = 1; for ( int j = 1; j <= i; j++) { System.out.print(num + "bb"); num *=3; } } System.out.println(); i++; } }

1bb 1bb3bb 1bb3bb9bb 1bb3bb9bb27bb

public class Test { public static void main( String[] args) { for (int i = 0; i < 5; i++) System.out.print((i + 4) + " "); } }

4 5 6 7 8

What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y +=i; } System.out.println(y);

45

What is the output produced by the following code segment? for (int x = 5; x > 0; x--) System.out.print(x);

54321

Analyze the following code. int x = 1; while (0 < x) && (x < 100) System.out.println(x++);

The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.

Analyze the following code: public class Test { public static void main(String[] args){ int i = 0; for (i = 0; i <10; i++); System.out.println(i + 4); } }

The program compiles despite the semicolon (;) on the for loop line, and displays 14.

Analyze the following code. int count = 0; while (count < 100) { //Point A System.out.println("Welcome to Java!"); count++; //Point B } //Point C

count < 100 is always false at Point C

Write a loop that computes (No need to write a complete program) (100/1)+(99/2)+(98/3)+(97/4)...+(3/98)+(2/99)+(1/100)

double i = 100; double j = 1; double compute = 0; for(j = 1, i = 100; j <= 100; j++, i--) { compute += (i/j); } System.out.println(compute);

If you enter input 9, show the output of the following code: import java.util.Scanner; public class Test { public static void main( String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt(); int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println("i is " + i); if (isPrime) System.out.println(number + " is prime"); else System.out.println(number + " is not prime"); } }

i is 4 9 is not prime

Write a program that counts the number of lowercase letters in a string. Your program should prompt the user to enter the string and display the count.

import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter the string: "); String s = input.nextLine(); int lowercase = 0; for(int i = 0;i < s.length();i++){ if(Character.isLowerCase(s.charAt(i))){ lowercase++; } } System.out.println("No. of lowercase letter : " + lowercase); input.close(); } }

Suppose you write the following program that prompts the user to enter the current year tuition and finds out how many years the tuition will be doubled. There is an error in the highlighted line. Fix it. You don't need to write the complete new program, just circle the affected code and replace it with the new code. import java.util.Scanner; public class Test { public static void main( String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the current year tuition: "); double tuition = input.nextDouble(); int year = 0; while (tuition < 2 * tuition) { tuition = tuition * 1.07; year++; } System.out.println("Tuition will be doubled in " + year + " years"); System.out.printf("Tuition will be $%.2f in %1d years ", tuition, year); } }

instead of: 2 * tuition double twiceTuition = tuition * 2 then do while (tuition < twice tuition)

Convert the following loop into a do-while loop. int sum = 0; for (int i = 0; i < 100; I++){ sum += i; }

int sum = 0; int i = 0; do { sum += i; i++; } while (i <100);

Show the output of the following code: Suppose that the input is 2 3 4 5 0. What is the output of the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number, max; number = input.nextInt(); max = number; while (number != 0) { number = input.nextInt(); if (number > max){ max = number; } System.out.println("max is " + max); System.out.println("number" = number) } } }

max is 5 number is 0

What is the number of iterations in the following loop: for (int i = 1; i <=n; i++){ //iteration }

n

Write a code segment, using a for loop, that will generate 10 random numbers in interval 1 to 10 inclusive [1, 10].

public class practice { public static void main(String[] args) { int random; for (int i = 1; i <= 10; i++) { random = 1 + (int)(Math.random() * 10); System.out.println(random); } } }

Write a complete program that prints numbers from 1 to 50, but if numbers that are multiple of 5. print HiFive, else if numbers that are divisible by 2, print HiTwo, and else if numbers that are divisble by 3 or 7, print HiThreeOrSeven

public class practice { public static void main(String[] args) { int random; for (int i = 1; i <= 50; i++) { random = (int)(Math.random() * 50) + 1; if (random % 5 == 0) System.out.println("HiFive"); else if (random % 2 == 0) System.out.println("HiTwo"); else if((random % 3 == 0) || (random % 7 == 0)) System.out.println("HiThreeSeven"); else System.out.println(random + " is not divisble by 5, 2, 3, or 7"); } } }

How many times if the following loop body repeated? What is the output of the loop? int i = 0; while (i < 10) { if ((i + 1) % 2 == 0) System.out.println(i); i++; }

repeated 10 times output: 1 3 5 7 9

What is i after the following for loop is finished? int y = 0; for (int i = 0; i<10; ++i){ y +=i; }

undefined


Kaugnay na mga set ng pag-aaral

EMT Chapter 14 - Medical Overview

View Set

Week 5: TXT 02-18 ¿Qué? o ¿Cuál? (p. 54)

View Set