CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Similar documents
CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Question: Total Points: Score:

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Question: Total Points: Score:

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

Question: Total Points: Score:

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Instructor: Yu Wang 11/16/2012

Question: Total Points: Score:

Question: Total Points: Score:

Place your name tag here

Question: Total Points: Score:

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Question: Total Points: Score:

Question: Total Points: Score:

CS 170 Exam 2. Section 004 Fall Name (print): Instructions:

CS 190 Exam 2 Fall 2014

Exam Percentage: / 55 = %

CS170 (005): Introduction to Computer Science Exam 2

CS 155 Exam 2 Spring 2015

CS 110 Exam 2 Spring 2011

CS 110 Exam 2 Fall 2011

Question: Total Points: Score:

CS 1301 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2010

CS 2316 Exam 4 Fall 2011

CS 1301 Exam 1 Fall 2011

CS 1301 Exam 1 Spring 2014

Your (printed!) Name: CS 1803 Exam 3. Grading TA / Section: Monday, Nov. 22th, 2010

CS 110 Exam 1 Fall 2011

CS 1301 Exam 1 Spring 2011

CS 190 Exam 1 Fall 2010

CS 2316 Exam 4 Fall 2012

CS Name : Grading TA:

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 2 Fall 2014

CS 2316 Exam 1 Spring 2013

CS 2316 Exam 4 Fall 2014

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 2 A Fall 2015

CS 1301 Exam 3 Spring 2014

CS Exam 2 - Spring 2010

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 2 A Fall 2015

CS Exam 3 - Spring 2010

CS 2316 Exam 4 Fall 2011

CS Exam 3 - Spring 2010

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CS 1301 Exam 3 Fall 2014

CS 2316 Exam 4 Fall 2012

CS 1803 Fall 2010 Exam 1

CS 1301 Exam 1 Fall 2013

CS 2316 Exam 1 Spring 2014

CS 2316 Exam 4 Spring 2013

Question Possible Points Earned Points Graded By GUI 22 SQL 24 XML 20 Multiple Choice 14 Total Points 80

King Saud University College of Computer and Information Sciences Computer Science Department

CS 1301 Exam 2 Fall 2010

CS 1803 Fall 2010 Exam 1

CS 1301 Exam 2 Fall 2013

Final Exam Practice. Partial credit will be awarded.

CS 113 PRACTICE FINAL

CS 1301 Exam 1 Fall 2013

CS 1301 Exam 1 Spring 2015

CS 1301 Exam 3 Spring 2012

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

1. Find the output of following java program. class MainClass { public static void main (String arg[])

CS 2316 Exam 3 Spring 2013

CS 1301 Exam 2 Spring 2014

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

CS 1301 Exam 3 A Spring 2015

CS 2316 Exam 3 Fall 2012

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

CS 111X - Fall Test 1

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Agenda: Discussion Week 7. May 11, 2009

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CS 2316 Exam 3 Fall 2011

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS 1316 Exam 1 Summer 2009

Selected Questions from by Nageshwara Rao

CS Exam 2 Name: Your Grading TA: This exam has 7 pages including the title page. Please check to make sure all pages are included.

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

CS 2316 Exam 4 Spring 2012

Practice with variables and types

H212 Introduction to Software Systems Honors

CS 1301 Exam 1 Fall 2009

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Transcription:

Name (print): INSTRUCTIONS: o Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. o Do NOT communicate with anyone other than the professor/proctor for ANY reason in ANY language in ANY manner. o This exam is not open notes, open books, and no calculator, no electronics. o Turn all mobile devices OFF and put them away now. You cannot have them on your desk. o Write neatly and clearly indicate your answers. If I cannot read your answer, I will assume to be incorrect. o Stop writing when told to do so at the end of the exam. I will take 10 points OFF if you keep writing after I told you to stop. o Academic misconduct will not be tolerated. Suspected academic misconduct will be immediately referred to the Emory Honor Council. Penalties for misconduct will be a zero on this exam, an F grade in the course, and/or other disciplinary action that may be applied by the Emory Honor Council. TIME: This exam has 5 questions on 9 pages including the title page. Page 10 is left blank and you can use it if you need more space to answer to any problem; make sure you include the number of the problem. Please check to make sure all pages are included. You will have a total of 60 minutes to complete the exam. Good luck! I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Emory community. I have also read and understand the requirements and policies outlined above. Signature: Question: 1 2 3 4 5 Total Points: 15 20 30 20 15 100 Score: 1

1. (15 points) Answer the following questions. Be concise and limit your response to 50 words. a. (1 point) What is a variable? b. (2 points) Give the names of 4 primitive data types available in Java. c. (2 points) Explain the usage of keywords break and continue. d. (3 points) Write down the appropriate answers to the following UNIX commands: Change the current directory to subdirectory subdir/ Go to parent directory List the content of current directory cd subdir/ cd.. ls 2

e. (7 points) For each row of the table, state whether the expression is correct or has an error. If there is no error, give the type and result of the expression. If there is error, leave the type and result as blank. The first row has been completed for you. int a = 10, b = 2; double c = 2.0, d = 3; char e = a, f = b ; String g = 123 ; Expression Error? Type (if no error) Result (if no error) a + b no int 12 (char)a + b no int 12 a > d no boolean true g + a * b no String 12320 g + f - e yes (int) d/c no double 1.5 e > a && f e == 1 no boolean true b < a <= 20 yes 1pt each row 2. (20 points) Assume the statements below are part of a Java program which compiles and runs. a. (14 points) What is the output if the user types 25? Scanner in = new Scanner(System.in); int num = in.nextint(); if (num > 10) { System.out.println("one"); else if (num > 20) { System.out.println("two"); else if (num > 40) { System.out.println("three"); else { System.out.println("four"); System.out.println("rose"); if (num > 40) { System.out.println("one"); else if (num > 20) { //one //rose 3

System.out.println("two"); else if (num > 10) { System.out.println("three"); else { System.out.println("four"); System.out.println("tulip"); if (num > 10) { System.out.println("one"); if (num > 20) { System.out.println("two"); if (num > 40) { System.out.println("three"); else { System.out.println("four"); //two //tulip //one //two //four one rose two tulip one two four 2 pts each word b. (6 points) What does the program output if theday variable has the following values? Answer for each case separately in the following table. theday WEDNESDAY 2 pts SATURDAY 2 pts SOMEDAY 2pts output Working for the man :) Ahh, the weekend... Ahh, the weekend... What day is it? final int MONDAY = 1; final int TUESDAY = 2; final int WEDNESDAY = 3; final int THURSDAY = 4; final int FRIDAY = 5; 4

final int SATURDAY = 6; final int SUNDAY = 7; final int SOMEDAY = 10; switch (theday) { case MONDAY: case TUESDAY: case WEDNESDAY: case THURSDAY: System.out.println("Working for the man :)"); break; case FRIDAY: System.out.println("Weekend is coming "); case SATURDAY: System.out.println("Ahh, the weekend..."); case SUNDAY: System.out.println("Ahh, the weekend..."); break; default: System.out.println("What day is it?"); 3. (30 points) ) Write down the output of each of the code below. For a syntactic error, if any, write syntax error and briefly explain it on the provided code. For infinite loop, write infinite loop and briefly explain why. a) (5 points) Infinite loop doesn t reach 0 while(x!= 0){ System.out.println( x = + x ); x -= 8; b) (5 points) Infinite loop x doesn t change while(x!= 0) System.out.println( x = + x ); x -= 10; 5

c) (5 points) X = 25 X = 20 while(x!= 0){ System.out.println( x = + x ); x -= 5; if (x == 15) break; d) (5 points) X = 25 X = 20 X = 15 while(x!= 0){ X = 10 System.out.println( x = + x ); X = 5 x -= 5; if (x == 15) continue; e) (5 points) Infinite loop ; after loop condition while(x!= 0);{ System.out.println( x = + x ); x -= 10; f) (5 points) Error missing in println while(x!= 0){ System.out.println(x = + x ); x -= 10; 6

4. (20 points) The web is built with HTML strings like "<i>yay</i>" which draws Yay as italic text. In this example, the "i" tag makes <i> and </i> which surround the word "Yay". Given tag and word strings, create a method (function) that takes tag and word as input parameters and returns the HTML string with tags around the word, e.g. "<i>yay</i>". Then, invoke this function with the tag <i> and word Yay, and then, with the tag <b> and word Java. //accept any method that is correct, but keep requirements 6 public static String taggy(string tag, String word){ 2 2 2 5 String newstr = < + tag + > + word + </ + tag + > ; //accept any correct form 3 return newstr; 3 String st = taggy( i, Yay ); 3 String str = taggy( b, Java ); 5. (15 points) Write code that prints out a multiplication table starting from 1 up to a number given by user. Assume that the number is a multiple of 10 (no need to check it!). Assume that between numbers there are 3 spaces. Print 10 numbers per each row as in the following example, where number is 20: 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 2 2 4 6 8 10 12 14 16 18 20 7

public class MultiplicationTable { public static void main(string[] args) { 1 Scanner in = new Scanner(System.in); int num = in.nextint(); //display the number title 1 System.out.print( ); 2 for (int j = 1; j <= 10; j++) System.out.print( + j); //display the table 2 int k = n / 10; 2 for (int i = 1; i <= k; i++){ 1 System.out.print(i + ); 2 for (int j = 1; j <= 10; j++) 3 System.out.printf( %4d, i*j); 2 1 1 System.out.println(); 8