The basis for education in the last millennium was “reading, writing, and arithmetic;” now it is reading, writing, and computing. Learning to program is an essential part of the education of every student, not just in the sciences and engineering, but in the arts, social sciences, and humanities, as well. Beyond direct applications, it is the first step in understanding the nature of computer science’s undeniable impact on the modern world.  This course covers the first half of our book Computer Science: An Interdisciplinary Approach (the second half is covered in our Coursera course Computer Science: Algorithms, Theory, and Machines). Our intent is to teach programming to those who need or want to learn it, in a scientific context.

You can enroll here:

Computer Science: Programming with a Purpose Week 2 Coursera Quiz Answers!

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;
}