Answering Questions
about Programming
CMPS 5J
January 23, 2019
Announcements
First external guest speaker confirmed (next Friday)
She will address some specific questions from L02:
Also, she will break down a programming project done for social activism, not for a job or for school.
Scoring for R05 (“Coding is not difficult”) will be slow
You only wrote two paragraphs,�but we now have 840 paragraphs to read.
Meanwhile, coding actually is difficult for most people most of the time if they keep advancing in the difficulty of projects they take on.
You don’t need to do all of Digital Information
You only need to do lessons 1-6 (the assignment on Canvas also mentions this).
When you get to 2.3.9 (Text to Binary) and you need to write a JavaScript program, feel free to use the code given on Canvas: https://canvas.ucsc.edu/courses/19599/discussion_topics/73151
Who is not on Piazza for this class? (163 of you?)
Click this link to sign up: https://piazza.com/ucsc/winter2019/cmps5j01
(this link can also be found on the syllabus and in a dedicated Canvas announcement)
Please fill out the course feedback form (just 5% have so far)
https://canvas.ucsc.edu/courses/19599/discussion_topics/72927
Some items for right now:
Current Events
Monday was MLKjr Day
The relevance of race and discrimination is sharply relevant to programming, particularly now in 2019.
Yesterday on Twitter
https://twitter.com/RealSaavedra/status/1087627739861897216
https://twitter.com/nke_ise/status/897756900753891328
Week 4 lab will focus on programming simple electronic devices.
Answering Questions
about Programming
This is what you’ll need to do on the exams in this class.
You’ll need conceptual knowledge and practical skills.
Which for-loop headers will execute a block 8 times?
for(int i = 0; i < 8; i++) // ? times
for(int i = 1; i <= 8; i++) // ? times
for(int i = 1; i < 8; i++) // ? times
for(int i = 0; i < 80; i+=10) // ? times
for(int i = -4; i < 4; i++) // ? times
What kind of Java construct is each underlined term?
class MySolution extends Solution
{
public int roundToTens(int x)
{
return 10 * (( x + 5) / 10);
}
}
Options: class, type, method, variable, keyword
Extended options: function, procedure
Not an option: private void
Which programs result in equivalent behavior?
class A extends SuperKarel
{
public void run()
{
move();
move();
turnLeft();
move();
turnRight();
move();
move();
move();
turnLeft();
move();
turnRight();
move();
}
}
class B extends SuperKarel�{� public void run()� {� for(int i = 0; i < 8; i++)� {� move();� }� for(int i = 0; i < 2; i++)� {� turnLeft();� turnRight();� }� }
}
class C extends SuperKarel�{� public void run()� {� step();� step();� }�� private void step()� {� move();� move();� turnLeft();� move();� turnRight();� move();� }�}
Pick the appropriate data type
You are a developer at video streaming company Youku, and you are working on the Android mobile app. The app is mostly written in the Java programming language.
You know that popular shows like Love O2O (a show about programmers in college, and more) can have over 24 billion views.
Which of Java’s integer types could accurately represent video view counts in the mobile app? Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Options: byte, short, int, long, float, double, char, boolean
What does this method do?
int f(int a, int b) �{ � if (a == 0) { return b; } � if (b == 0) { return a; }� if (a == b) { return a; }� � if (a > b) {� return f(a-b, b);� } else {� return f(a, b-a); � }�}
Options:
What’s the biggest problem?
public int pow(int x, int y)�{� int result = x;� while(y > 1)� {� if (y % 2 = 0) // y is even� {� result *= result;� }� else� {� result *= result * x; � }� y /= 2;� }� return result;�}
Your friend is implementing a method that will compute x to the y-th power, but something is wrong. What the biggest problem?
Options:
Translate from Python (or another language with Java-like structures)
def countFactors(x, n):� num = 0� while x % n == 0:� num += 1� x /= n� return num��print(countFactors(518400,3))
Translate this simple program from Python into Java. What does it print?
Options: 8, 4, 0, 5