Java writing programs Questions 1 For each of the following

Java writing programs:

Questions 1. For each of the following programs, determine whether or not they are valid Java programs without typing them in. If they are valid, indicate what they output. If not, explain why (be precise).

(a) public class Shadow1 { public static void main(String[] args) { int i = 0; System.out.println(i); int j = 1; System.out.println(j); int i = j; System.out.println(i); } }

(b) public class Shadow2 { public static void main(String[] args) { int i; for (int i = 0; i < 10; i++) { System.out.println(i); } } }

(c) public class Shadow3 { static private int i = 99; public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(i); } System.out.println(i); } }

(d) public class Shadow4 { private int i; public void doSomething(int n) { for (int i = 0; i < n; i++) { System.out.println(i); } } public static void main(String[] args) { new Shadow4().doSomething(9); } }

(e) public class Shadow5 { private int i; public void doSomething(int i) { for (int i = 0; i < 10; i++) { System.out.println(i); } } public static void main(String[] args) { new Shadow5().doSomething(9); } }

(f) public class Shadow6 { private int i; public void doSomething(int i) { for (i = 0; i < 10; i++) { System.out.println(i); } } public static void main(String[] args) { new Shadow6().doSomething(9); } }

(g) public class Shadow7 { private int i = 99; public void doSomething(int i) { for (i = 0; i < 10; i++) { System.out.println(i); } } public static void main(String[] args) { Shadow7 i = new Shadow7(); i.doSomething(99); System.out.println(i.i); } }

2. Without typing it in and running it, what is the output of the following program? Of course we can’t tell if you type it in or not. But remember that on a quiz or exam, you won’t be able to type it in. So the goal is to understand why it prints what it does. Test your answer by running the code. If you got the wrong output, use println statements to trace the program’s execution until you understand it. You can also make up your own variations and test yourself further. public class ScopeTester { protected int num; protected String str; static final String[] labels = { \"Hello\", \"Goodbye\", \"Whatever\" }; public ScopeTester(int num, String str) { this.num = num; this.str = str; } public void printout() { System.out.println(num + \" \" + str); } public void doubleMe() { num *= 2; str += \" \" + str; } public int twoTimes(int num) { num *= 2; return num; } public static void main(String[] args) { for (int num=0; num < 3; num++) { ScopeTester tester = new ScopeTester(num, labels[num]); int num2 = tester.twoTimes(num); tester.doubleMe(); tester.printout(); } } }

Solution

a) Error as you have already declared int i=0 (integer type), now you cannot use int i = j , means just remove \"int\" before \"i\" in this line and then the program will run perfectly.

b) Error as \"i\" is declared as of integer type (int = 0) , so remove \"int\" before \"i\" , then it will run.

c) it will run fine and the output is

0
1
2
3
4
5
6
7
8
9
99

d) program will run fine and output is

0
1
2
3
4
5
6
7
8

e) Error as \"i\" is declared as of integer type (int = 0) , so remove \"int\" before \"i\" , then it will run.

f) Program will run perfectly and the output is

0
1
2
3
4
5
6
7
8
9

g) Program will run perfectly and the output will be

0
1
2
3
4
5
6
7
8
9
99

2) The program will work fine and work as per the logic written and whatever is written inside the system.out.println will get printed and the output of the program is :

0 Hello Hello
2 Goodbye Goodbye
4 Whatever Whatever

Java writing programs: Questions 1. For each of the following programs, determine whether or not they are valid Java programs without typing them in. If they ar
Java writing programs: Questions 1. For each of the following programs, determine whether or not they are valid Java programs without typing them in. If they ar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site