Q2: Which of the following are valid Java identifiers? If not explain why

Similar documents
System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Introduction to Java

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Part I. Multiple Choice Questions (2 points each):

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Sample CSE8A midterm Multiple Choice (circle one)

AP Computer Science Java Mr. Clausen Program 9A, 9B

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Arrays in Java. Working with Arrays

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 2 Elementary Programming

Install Java Development Kit (JDK) 1.8

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

In this Chapter you ll learn:

Basics of Java Programming Input and the Scanner class

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Building Java Programs

JAVA ARRAY EXAMPLE PDF

Building Java Programs

Introduction to Programming

AP Computer Science Static Methods, Strings, User Input

Java Crash Course Part I

Introduction to Java. CS 3: Computer Programming in Java

CS 106 Introduction to Computer Science I

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

J a v a Quiz (Unit 3, Test 0 Practice)

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 2 Introduction to Java programming

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Object-Oriented Programming in Java

Crash Course in Java

Java Basics: Data Types, Variables, and Loops

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Data Structures Lecture 1

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Building Java Programs

Programming Languages CIS 443

Using Files as Input/Output in Java 5.0 Applications

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

6.1. Example: A Tip Calculator 6-1

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Topic 11 Scanner object, conditional execution

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Chapter 2: Elements of Java

AP Computer Science Java Subset

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

COMPUTER SCIENCE. Paper 1 (THEORY)

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Homework/Program #5 Solutions

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Conditionals (with solutions)

More on Objects and Classes

Programming in Java Course Technology, a part of Cengage Learning.

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

(Eng. Hayam Reda Seireg) Sheet Java

Chapter 3. Input and output. 3.1 The System class

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Java Program Coding Standards Programming for Information Technology

Lab Experience 17. Programming Language Translation

Java CPD (I) Frans Coenen Department of Computer Science

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Arrays. Introduction. Chapter 7

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Semantic Analysis: Types and Type Checking

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Building Java Programs

Lecture 1 Introduction to Java

Example of a Java program

Passing 1D arrays to functions.

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

13 File Output and Input

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Introduction to Object-Oriented Programming

Statements and Control Flow

Moving from CS 61A Scheme to CS 61B Java

Lecture 22: C Programming 4 Embedded Systems

Java Interview Questions and Answers

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Explain the relationship between a class and an object. Which is general and which is specific?

Line-based file processing

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

An Incomplete C++ Primer. University of Wyoming MA 5310

Programmierpraktikum

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

LAB4 Making Classes and Objects

Chapter 6: Programming Languages

Iteration CHAPTER 6. Topic Summary

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Transcription:

Java Programming 1 (Revision) Name: Section#: ID: Q1: True/false 1. A Java identifier can start with a digit. (F) cannot 2. Hello, HELLO, and hello are considered as different identifiers. (T) 3. The name of a Java program file must end with the extension.class (F).java 4. Comments are not ignored by the compiler and they cause the computer to print the text after the // on the screen when the program executes. (F) 5. The number of arguments in the method call must match the number of parameters in the method declaration s parameter list. (T) 6. An array can store many different types of values. (F) Q2: Which of the following are valid Java identifiers? If not explain why 1. Quiz1 valid 2. first_student valid 3. Sara s_book invalid since it contains an apostrophe. 4. 2Two invalid since it starts with a digit 5. _a valid 6. Thirty_Five valid 7. char invalid since char is reserved word 8. $R3 valid 9. midterm-grade invalid since it contains dashes. Q3: Fill in the blank: 1. The programs that translate high-level language programs into machine language are called. compilers 2. Return type indicates that a method will not return a value. void 3. A variable known only within the method in which it s declared is called a(n). local variable 4. An array is a group of variables (called elements or components) containing values that all have the same. type T.A. Arwa Alromih Page 1

5. The number used to refer to a particular array element is called the element s. index 6. An array that uses two indices is referred to as a(n) array. two-dimensional 7. The name of the element in row 3 and column 5 of array d is. d[3][5] Q4: Choose the correct answer: 1. High-level programming languages are (easier, harder) to understand and program than low-level ones or assembly language. 2. Java is considered a (high level language, assembly language, low-level language). 3. Comments can be written using ( //, \\, ). 4. if (7 <= 7) System.out.println(6-9 * 2 / 6); outputs the following: i. -1 ii. 3 iii. 3.0 iv. none of these 5. if (5 < 3) System.out.println("*"); if (7 == 8) System.out.println("&"); System.out.println("$"); outputs the following: i. * ii. & iii. $ iv. none of these Q5: Show the value of x after each statement is performed: a) x = 7 + 3 * 6 / 2-1; x=15 b) x = 2 % 2 + 2 * 2-2 / 2; x=3 c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ); x=324 Q6: Give the method header for each of the following methods: 1. Method hypotenuse, which takes two double-precision, arguments side1 and side2 and returns a double-precision, result. public static double hypotenuse (double side1, double side2) 2. Method smallest, which takes three integers x, y and z and returns an integer. public static int smallest (int x, int y, int z) T.A. Arwa Alromih Page 2

3. Method instructions, which does not take any arguments and does not return a value. [Note: Such methods are commonly used to display instructions to a user.] public static void instructions () 4. Method inttofloat, which takes integer argument number and returns a float. public static float inttofloat (int a) Q7: Find the errors in the following programs and state whether it's logical or syntax error: Code // 1 error public class cs public static void main(string args[]) int a=5, b=5,c=0; if(a++>0) c++; c--; //1 error public class cs public static void main(string args[]) int a=5, b=5,c=0; Error Type syntax Correction closing after if and before if(a++>0) c++; c--; syntax if (a++>b && b >0) c++; if(a++>b>0) c++; c--; // this loop should read 11 numbers from the user int num; for(int i =0; i<10;i++) num = read.nextint(); //this loop print numbers from 10 to 0 in descending order int n= 0; while( n >= 0) System.out.println(n); n--; //2 errors int[] A=new int[10]; for(int i=0;i<a.length();i++) A(i)=i*2; public static void p( ) int a=4,b=6,c=5; return a*b*c; public static int sum(int x,int y) double result =x+y; return result; logical for(int i =0; i<=10;i++) num = read.nextint(); logical int n= 10; syntax syntax syntax for(int i=0;i<a.length;i++) A[i] = i*2; should return int return type should be double T.A. Arwa Alromih Page 3

Q8: What is the output of the following java code segment: int a = 6; if (a > 0) switch (a) case 1: System.out.println( a + 3); case 3: System.out.println( a++); case 6: System.out.println( a + 6); case 8: System.out.println( a * 8); default: System.out.println( a ); System.out.println( a + 2); double[] array = new double[4]; double mul = 1; array[0] = 2.0; array[1] = array[0]*array[0]+5-3; array[2] = array[0]+ array[1]/2; array[3] = 2.0; for (int i = 0; i < array.length; i++) System.out.print(array[i] + " \n"); mul *= array[i]; System.out.print("the multiplication is :" + mul); 12 48 2.0 6.0 5.0 2.0 the multiplication is :120.0 Q9. What is the value of x after each of the following statements is executed a) x = Math.abs(-123); 123 b) x = Math.floor(-5.1); -6 c) x = Math.ceil(-5.1); -5 d) x = Math.ceil(3.0); 3 e) x = Math.abs(-3.77); 3.77 f) x = Math.pow(8,2); 64 g) x =(-Math.abs(-8 + Math.round(4.5))); -3 T.A. Arwa Alromih Page 4

Q10: Which of the following method headings are valid? If they are invalid, explain why. a. public static int method1( ) valid b. public static char for(int a, int b) invalid, for is a reserved word c. public static void (int a, int b) invalid, missing method name d. public static double method2(double x,y) invalid missing second parameter type e. public static methodx() invalid, missing return type f. public static double method2(string ) invalid, missing parameter name. Q11: Convert the if.. statement into Conditional operator (? : ) if (a >= b) max = a; max = b; max = (a >= b)? a : b; if (rank <60) System.out.println("Rank B"); System.out.println("Rank A"); System.out.println( (rank <60)? "Rank B" : "Rank A"); Q12: Convert the while loop into for loop: x = 5; y = 35; while (x < y) System.out.println(x + " " + y); x = x + 10; x = 5; y =35 for (x=5; x<y; x+=10) System.out.println(x + " " + y); T.A. Arwa Alromih Page 5

Q13: Convert the for loop into while loop: for (i = 0; i <= 5; i++) System.out.print("*"); i=0; while (i<=5) System.out.print("*"); i++; Q14: Convert the switch statement into if.. statements: switch (grade) case 'A': System.out.println("The grade is A."); case 'B': System.out.println("The grade is B."); case 'C': System.out.println("The grade is C."); case 'D': System.out.println("The grade is D."); case 'F': System.out.println("The grade is F."); default: System.out.println("The grade is invalid."); if (grade == 'A') System.out.println("The grade is A."); if (grade == 'B') System.out.println("The grade is B."); if (grade == 'c') System.out.println("The grade is C."); if (grade == 'D') System.out.println("The grade is D."); if (grade == 'F') System.out.println("The grade is F."); System.out.println("The grade is invalid."); T.A. Arwa Alromih Page 6

Q15: Convert the following array declaration into the shorthand notation: int[] a = new int[5]; a[0] = 0; a[1] = 1; a[2] = 2; a[3] = 3; a[4] = 4; int[] a = 0,1,2,3,4; int[][] a = new int[2][3]; a[0][0]=0; a[0][1]=1; a[0][2]=2; a[1][0]=3; a[1][1]=4; a[1][2]=5; a[2][0]=6; a[2][1]=1; a[2][2]=8; int[][] a =0,1,2,3,4,5,6,7,8; Q16: Programming Challenges: 1. Write a method that takes an array of ints as an argument, plus two additional int arguments called pos1 and pos2. The method should swap the elements of the array at positions pos1 and pos2. public static void swap(int [] arr, int pos1, int pos2) int temp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = temp; 2. Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers the program should stop when the user enters 0 public class sumoddeven static java.util.scanner input; public static void main(string[] args) input = new Scanner(System.in); int number = 0; // variable to store the new number int sumodds = 0; // variable to store the sum of odd integers int sumevens = 0; // variable to store the sum of even integers //ask for the user input System.out.println("Please enter integers, 0 is the sentinel value"); do //get the input number = input.nextint(); //determine whether the number is even or odd if (number % 2 == 0) T.A. Arwa Alromih Page 7

sumevens = sumevens + number; sumodds = sumodds + number; while(number!= 0); // endloop //print out the result message System.out.println("Even sum: " + sumevens); System.out.println("Odd sum: " + sumodds); 3. Write a program that reads five numbers from the user and stores them in an array, then find their sum, and print the numbers in the reverse order. import java.util.*; public class ReversePrint static Scanner input = new Scanner(System.in); public static void main(string[] args) int[] items = new int[5]; //declare array item of five elements int sum; System.out.println("Enter five integers:"); sum = 0; for (int i = 0; i < items.length; i++) items[i] = input.nextint(); sum = sum + items[i]; System.out.println("The sum of the numbers = " + sum); System.out.print("The numbers in the reverse "+ "order are: "); //print the numbers in the reverse order for (int i = items.length - 1; i >= 0; i--) System.out.print(items[i] + " "); System.out.println(); T.A. Arwa Alromih Page 8