Computer Science: Programming with a Purpose Week 2 Quiz Answer

Computer Science Programming with a Purpose Week 2 Quiz Answer

Computer Science: Programming with a Purpose Week 2 Quiz Answer


In this article i am gone to share Coursera Course Computer Science: Programming with a Purpose Week 2 | Conditionals and Loops Quiz Answer with you..




Conditionals and Loops


Question 1)
What does the following program print?

public static void main(String[] args)
{
    int a = 25
    int b 3;
    int c 4;
    if (a < b)
    {
       if (b < c)
       {
          if (c < a) System.out.println (a +" " + b+c);
          else System . out . println (c +" " + b +""+ a);
}
        else System.out.println(a + " " +C +" " + b);
    }
    else System . out.println (b +"* *" +c)
}

  • 234
  • 243
  • 324
  • 342
  • 423
  • 432


Question 2)
Which of the following will lead to a compile-time error? Mark all that apply.

  • if a > b, then c = 0;
  • if a > b {c = 0; }
  • if (a > b) c = 0 else b = 0; 
  • if (a > b) c = 0;
  • if (a > b) (c = 0; )
  • if (a> b) then c = 0;
  • if (a > b) { c - 0;}



Question 3)
Which of the following errors will be detected at compile time? Mark all that apply.
  • Missing left curly brace
  • Missing semicolon at the end of a statement
  • Trying to subtract two strings
  • Missing curly braces around a sequence of statements in a while loop
  • Division by a variable that might be zero
  • Forgetting to declare the type of a variable



Question 4)
What is the value of m after executing the following code?

int n = 123456789;
int m = 0;
while (n != 0)
{
  m = (10 * m) + (n % 10);
  n = n / 10;
}


  • 987654321



Question 5)
Suppose that i andj are both of type int. What is the value of j after the following statement is executed?

for (i = 0, j = 0; i <10; i++)
j += i;


  • 45










Post a Comment

0 Comments