Question

In: Computer Science

2. What are the three program control structures? Define each thoroughly and give an example of...

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.

Solutions

Expert Solution

2.Three program control structures are:

1.Conditional branches.

  • This kind of control structure is used to select a path from the available two or more paths.
  • It is further divided as if/else/else if, ternary operator, and switch.

Examples:

  • if/else/if else:

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”);

}

  • Ternary operator:

System.out.println(a>b ? “a is greater than b”: “a is smaller or same as b”);

  • Switch:

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

  • This kind of control structure is used to iterate a specified number of instructions a particular number of times.
  • It includes ‘while loop’, ‘For loop’, and ‘do-while’ loop.

Examples:

  • while loop:

while (i>0)

{

System.out.println(“c is 3”);

}

  • For loop:

for (i=0;i<n;i++)

{

System.out.println(“c is 2”);

}

  • do-while loop:

do

{

System.out.println(“ c is 1”);

}while (i<n);

3.Branching statement:

  • Branching statements are used to alter the flow of execution in loops.
  • It includes ‘break’ and ‘continue’.

Examples:

  • break:

We can exit early from a loop.

if(c==1)

{

System.out.println(“ c is 1”);

break;

}

  • continue:

Skip the rest of the loop.

if(c==1)

{

Continue;

System.out.println(“c is 2 ”);

}

System.out.println(“c is 1”);

3.Differences:

  • Class variables are declared with static keywords. An instance variable is declared with a 'new' keyword'.
  • Class variables are created when the program starts. But instance variables are created when the object is created.
  • Class variables can be assessed by calling its name along with the class name. But the instance variable can access directly by calling its name.
  • In java, we declare the class variables as:

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.

  • Java programs are “write once, run anywhere”.
  • The java programs are once written can be run on any platform.
  • Java is platform-independent.
  • Java programs are slower than C++ programs. So option b is incorrect.
  • C# does not require programmers to manage almost all memory operations. So option c is incorrect.
  • So true statement is the option a.

Related Solutions

Define each of Weber's three types of authority and give an example of each.
Define each of Weber's three types of authority and give an example of each.
Define the three forms of the efficient market hypothesis. Give an example of each.
Define the three forms of the efficient market hypothesis. Give an example of each.
Which are the three control structures used in java programming. Which of these three control structures...
Which are the three control structures used in java programming. Which of these three control structures is used in every program or application
1. What are the three types of risk? Give an example for each.
1. What are the three types of risk? Give an example for each. 2. List the four steps in planning for your insurance program. 3. Outline a personal insurance plan with the following phases: a) Identify personal, financial and property risks; b) Set goals you might achieve when obtaining needed insurance coverages; and c) Describe actions you might take to achieve these insurance goals.
Define implicit and explicit goals. Give an example of each.
Define implicit and explicit goals. Give an example of each.
Define endocrine, paracrine, and autocrine and give example of each.
Define endocrine, paracrine, and autocrine and give example of each.
7. What are the four “Market Structures”? Please describe each Market Structure and give an example...
7. What are the four “Market Structures”? Please describe each Market Structure and give an example of a firm that belongs in each of the four Market Structures, preferably a firm that was not discussed previously. 8. What is a “natural monopoly”? Please give an example. 9. Why are natural monopolies often regulated by some sort of Government Regulatory Agency?   10. In YOUR OPINION, SHOULD THE STATE OF CALIFORNIA HAVE TAKEN OVER PG&E? Why or why not?
Que) define in detail and give an example of the 3 basic tools to control simple...
Que) define in detail and give an example of the 3 basic tools to control simple learning 1) Reinforcement strengthens responses 2) Nonreinforcement causes responses to extinguish 3) Punishment suppresses responses.
What are the goals of production control? Give each of them a concrete example from business...
What are the goals of production control? Give each of them a concrete example from business or an industry.
What are the three methods used to control the money supply? Give specifics of each.
What are the three methods used to control the money supply? Give specifics of each.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT