Reference no: EM132176865
1. What output is produced by the following program?
public class Odds {
public static void main(String[] args) {
printOdds(3);
printOdds(17/2);
int x = 25;
printOdds(37 - x +1);
}
public static void printOdds(int n) {
for (int i = 1; i <= n; i++) {
int odd = 2*i -1;
System.out.print(odd + " ");
}
System.out.println();
}
}
What output is produced by the following program?
public class Weird {
public static void main(String{ } args) {
int number = 8;
halfFun(11);
halfFun(2 - 3 + 2 * 8);
halfFun(number);
System.out.println("number = " + number);
}
public static void halfFun(int number) {
number = number/2;
for (int count = 1; count <= number; count++) {
System.out.print(count + " ");
}
System.out.println();
}
}
3. What is the output of the following program?
public class MysteryTouch{
public static void main(String{ } args) {
String head = "shoulders";
String knees = "toes";
String elbow = head;
String eyes = "eyes and ears";
String ear = "eye";
touch(ear, elbow);
touch(elbow, ear);
touch(head,"elbow");
touch(eye, eye);
touch(knees, "Toes");
touch(head, "knees "+knees);
}
public static void touch(String str1, String str2) {
System.out.print("touch your " + str1+ " to your "+ str2););
}
}
4.what method called printStrings that accepts a string and a number of repetitions as parameters and prints that string the given number of times with a space after each time. For example, the call
printStrings("abc", 5)
will print the following output:
abc abc abc abc abc