0% found this document useful (0 votes)
59 views

Diploma in Information and Communication Technology: Module 3: Control Structure Tutorial

The document contains questions about control structures in Java including if/else statements, for loops, while loops, do/while loops, switch statements, and the break and continue keywords. It asks the reader to trace code snippets, determine outputs, rewrite code using different control structures, and describe what various keywords are used for.

Uploaded by

Zack Chong
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Diploma in Information and Communication Technology: Module 3: Control Structure Tutorial

The document contains questions about control structures in Java including if/else statements, for loops, while loops, do/while loops, switch statements, and the break and continue keywords. It asks the reader to trace code snippets, determine outputs, rewrite code using different control structures, and describe what various keywords are used for.

Uploaded by

Zack Chong
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DIPLOMA IN INFORMATION AND COMMUNICATION TECHNOLOGY

Module 3 : Control Structure Tutorial

1. Suppose you have the following declarations :


boolean found = false; boolean flag = true; double x = 6.2; double y = 4.6; int a = 15, b = 3; int n = 12; char ch = B;

State the value for the following logical expression.


a) b) c) d) e) f) g) h) i) j) k) l) !found x > 4.0 2 < 4 2 < 4 !found && ( x >= 0) ! (found && (x >=0)) x + y < = 20.5 (n >=0 ) && ( n <=100) (A <= ch && ch <= Z) (a + 2 <= b) && !flag 2 * 2 - 3 > 2 && 4 - 2 > 5 2 * 2 - 3 > 2 || 4 - 2 > 5

2. Trace the output for the following statements : a) int a=10; if (a > 5) System.out.println(Hello, good morning); else System.out.println(Good bye); b) int a = 10, b=5; if (a < 2 * b) System.out.print (Hello ); System.out.print (There); c) int x = 100; int y = 200; if (x > 100 && y <= 200) System.out.println (x + + y + + (x+y)); else System.out.println (x+ +y+ +(2 * x - y));

DIPLOMA IN INFORMATION AND COMMUNICATION TECHNOLOGY 3. Trace the output for the following java code segments :
(a)

(b)

4. Use a switch statement to rewrite the following java code segment:


if (a == 1) x += 5; else if (a == 2) x += 10; else if (a == 3) x += 16; else if (a == 4) x += 34;

DIPLOMA IN INFORMATION AND COMMUNICATION TECHNOLOGY 5. What is the value of alpha after the following Java code segment executed? If alpha has initial value of : (a) 1 (b) 5 (c) 0

6. Trace the output for the following java code segment: (a) j = 2;
for ( i = 0; i<=5; i++) { System.out.print(j + ); j = 2 * j + 3; }

(b)
int x = 5, y = 50; do x = x + 10; while ( x < y); System.out.println ( x + + y);

DIPLOMA IN INFORMATION AND COMMUNICATION TECHNOLOGY

7. Suppose that the input is :

38 45 71 4 -1
What is the output of the following code ? Assume all variables are properly declared.
(a)

(b)

8. What is the keyword break for? What is the keyword continue for? Will the following program terminate? If so, give the output. (a)

(b)

DIPLOMA IN INFORMATION AND COMMUNICATION TECHNOLOGY

(c)

(d)

9. Can you always convert a while loop into a for loop? Convert the following while loop into a for loop. int i = 1; int sum = 0; while (sum < 10000) { sum = sum + i; i= i + 2; }

You might also like