Difference between revisions of "Control Structures Review Questions and Exercises"

From CompSciWiki
Jump to: navigation, search
(changed question 3 to remove loop requirement)
(Removed all old examples and provided links to relevant Program-A-Day questions.)
Line 1: Line 1:
{{Template:1010Topic|Chapter_TOC=[[Control Structures]]|Introduction=The solutions to these exercises can be found [[Control Structures Solutions|here]].|Overview=These questions will test your knowledge of control structures.}}
+
{{Template:1010Topic
 
+
|Chapter_TOC=[[Control Structures]]
 +
|Body=
 
===Review Questions===
 
===Review Questions===
#Name the 3 different logical operators. What are the benefits of using logical operators?
+
Here are some Program-A-Day questions that will help you practice coding using the control structures learned in this chapter:
#What is the proper way in Java to compare strings? Why?
+
#[[Days In A Month|Days In A Month]]
#What are the results of the following logical operations?
+
#[[The Labyrinth|The Labyrinth]]
## 'a' != 'b'
+
#[[Know Your Standards|Know Your Standards]]
## (127-3*9) >= (97+8/4)
+
#[[Letter Grade Conversion|Letter Grade Conversion]]
## "Hello" == "Hello"
+
}}
#Write the truth table for the expression (X || Y) && Z
+
 
+
 
+
===Exercises===
+
1. Look at the code below. Clean it up so it uses the proper logical operators and proper use of if-else's.
+
 
+
<PRE>
+
if (x == 1)
+
{
+
  if (y == 1)
+
  {
+
    if (!!!!true)
+
    {
+
      System.out.println("X and Y are 1!");
+
    }
+
  }
+
}
+
if (x == 2)
+
{
+
  System.out.println("X is 2!");
+
}
+
if (x < 1)
+
{
+
  System.out.println("X is under 1 or over 2!");
+
}
+
if (x > 2)
+
{
+
  System.out.println("X is under 1 or over 2!");
+
}
+
</PRE>
+
 
+
 
+
 
+
2. Is the result of the following statement true or false?
+
 
+
<PRE>
+
String s1 = "test";
+
String s2 = "TEST";
+
String s3 = "test";
+
 
+
if (!(!(s1.equals(s2) && s1.equals(s3)) || s2.equals(s3)))
+
  System.out.println("The result is true");
+
else
+
  System.out.println("The result is false");
+
</PRE>
+
 
+
3. Write a program that will prompt a user to enter an integer. The program will determine whether the number is even or odd and print out the result. It will also print out whether or not the number is positive, negative, or zero.
+
 
+
4. Write a program that will prompt the user to enter a string.  The program will print out whether or not the first character is a vowel or not.
+
 
+
{{Category:COMP 1010}}
+
{{Category:Sample Questions}}
+
{{Category:Java}}
+

Revision as of 18:40, 4 December 2011

COMP 1010 Home > Control Structures


Review Questions

Here are some Program-A-Day questions that will help you practice coding using the control structures learned in this chapter:

  1. Days In A Month
  2. The Labyrinth
  3. Know Your Standards
  4. Letter Grade Conversion