This is the test for one of the sections:
- (25%) What does the following program print out when run?
public static void main(String[] args) { boolean bool = true; int i = 0; for (i=0; i < 5; i++){ System.out.println(i); if (bool && (i % 2 == 0) ) System.out.println("Ob"); else if (i < 3) System.out.println("La"); else if (i % 2 == 0 || i > 3 ) System.out.println("Di"); } System.out.println(i < 10); }
- (25%) What does the following program print out when run?
public static void main(String[] args) { String word = "CCBA"; while (word.length() > 0){ char c = word.charAt(0); System.out.print(c); //notice: print word = word.substring(1); int x = 0; switch (c) { //watch out for missing breaks! case 'A': x = 4; case 'B': x = 2; break; case 'C': x = 3; }; for (int i=0; i < x; i++){ System.out.print("*"); //print (NOT println) } System.out.println(""); //move to next line } }
- (25%) Write a program that asks the user to enter a
word and then prints YES if the first 3 characters of the word
are the same as the last 3 characters (and in the same order).
Otherwise, or if the word has fewer than 3 characters, the
program prints NO. Some sample runs:
Enter word:alphabet NO ---we run the program again--- Enter word:abalataba YES ---we run the program again--- Enter word:javaisjav YES ---we run the program again--- Enter word:javaisajv NO ---we run the program again--- Enter word:jav YES ---we run the program again--- Enter word:b NO
Answer:
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter word:"); String word = keyboard.next(); if (word.length() < 3) System.out.println("NO"); else if (word.substring(0,3).equals(word.substring(word.length() - 3))) { System.out.println("YES"); } else System.out.println("NO"); }
- (25%) Write a program that ask the user to enter a
sentence and then prints out that same sentence but with every
other character turned into its uppercase equivalent. For
example:
Enter message:this program is impossible to write tHiS PrOgRaM Is iMpOsSiBlE To wRiTe ---we run the program again--- Enter message:fun in the sun fUn iN ThE SuN
Answer:
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter message:"); String msg = keyboard.nextLine(); String result = ""; for (int i=0; i < msg.length(); i++){ String letter = msg.substring(i,i+1); if (i%2 == 0) { result += letter; } else { result += letter.toUpperCase(); } } System.out.println(result); }
- (25%) What does the following program print out when run?
public static void main(String[] args) { int i = 5; int sum = 0; for (i=5; i > 0; i--){ System.out.println(i); sum += (i * 2); if (i > 2 && i < 4) System.out.println("Ob"); else if (i % 3 > 0) System.out.println("La"); } System.out.println(sum); }
- (25%) What does the following program print out when run?
public static void main(String[] args) { String word = "ABCCAB"; while (word.length() > 0){ char c = word.charAt(0); System.out.print(c); //notice: print word = word.substring(2); int x = 0; switch (c) { //watch out for missing breaks! case 'A': x = 3; case 'B': x = 1; break; case 'C': x = 4; }; if (x <= 1) System.out.println(" alan"); else if (x <= 3) System.out.println(" turing"); else System.out.println(" lives"); } }
- (25%)
Write a program that asks the user to enter a word. If the
word has an 'a' in it then the program will print out the letter
that comes after the first 'a' in the word (from
left-to-right). If there are no 'a's or if the 'a' is the last
character in the word then the program will print out
"OOPS". Below are some sample runs.Enter word:eventful OOPS ---we run the program again--- Enter word:java v ---we run the program again--- Enter word:nova OOPS ---we run the program again--- Enter word:animation n
Answer:
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter word:"); String word = keyboard.next(); int aIndex = word.indexOf('a'); if (aIndex == -1) { System.out.println("OOPS"); } else if (aIndex == word.length() -1 ){ System.out.println("OOPS"); } else { System.out.println(word.charAt(aIndex + 1)); } }
- (25%) Write a program that asks the user to enter a
sentence. The program will then print out the total number of
times the letter 'a' appears in the sentence, as well as the total number of
times the two letters 'an' appear (in that order). Below are
some sample runs.Enter message:banana Number of As = 3 Number of ANs = 2 ---we run the program again--- Enter message:the music of the night Number of As = 0 Number of ANs = 0 ---we run the program again--- Enter message:are you suggesting that coconuts migrate? Number of As = 3 Number of ANs = 0
Answer:
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter message:"); String msg = keyboard.nextLine(); int numAs =0; int numANs = 0; for (int index = 0; index < msg.length(); index++){ if (msg.charAt(index) == 'a') { numAs++; //take advantage of short-circuit evaluation if (index + 1 < msg.length() && msg.charAt(index + 1) == 'n') numANs++; } } System.out.println("Number of As = " + numAs); System.out.println("Number of ANs = " + numANs); }
No comments:
Post a Comment