In: Computer Science
2. What are the three program control structures? Define each thoroughly and give an example of each.
3. Explain in detail the difference between a Class Variable and an Instance Variable. How do we declare a Class Variable?
4. Which of the following is true?
a. Java programs can run on different platforms, like
Windows and Unix, without being recompiled.
b. C++ programs have always run more slowly than Java
programs.
c. In contrast to Java, C# requires programmers to
manage almost all memory operations.
d. All of the above.
2.Three program control structures are:
1.Conditional branches.
Examples:
If (c==2)
{
system.out.println(“ c is 2”);
}
else if (c==1)
{
System.out.println(“ c is 1”);
else
{
System.out.println(“ c is not 2 and 1”);
}
System.out.println(a>b ? “a is greater than b”: “a is smaller or same as b”);
Int c=3;
switch(c)
{
case 0:
system.out.println(“ c is 1”);
break;
case 1:
System.out.println(“c is 2”);
break;
case 2:
System.out.println(“c is 3”);
break;
}
2.Loops
Examples:
while (i>0)
{
System.out.println(“c is 3”);
}
for (i=0;i<n;i++)
{
System.out.println(“c is 2”);
}
do
{
System.out.println(“ c is 1”);
}while (i<n);
3.Branching statement:
Examples:
We can exit early from a loop.
if(c==1)
{
System.out.println(“ c is 1”);
break;
}
Skip the rest of the loop.
if(c==1)
{
Continue;
System.out.println(“c is 2 ”);
}
System.out.println(“c is 1”);
3.Differences:
public class Varexamp
{
Int var;
static int data=10;
public static void main (String args[])
{
Varexamp obj = new Varexamp();
}
}
4.a.Java programs can run on different platforms, like Windows and Unix, without being recompiler.