Chapter 4 Arrays & Strings

Similar documents
Selected Questions from by Nageshwara Rao

Chapter 8 Threads Zindell Technologies, Ltd. Question 1: Which one statement below is true concerning the following code?

Getting started with Java

Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

CS171:Introduction to Computer Science II

- Thus there is a String class (a large class)

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

03-Dec-09. Boaz Kantor, IDC Herzliya. Boaz Kantor, IDC Herzliya

Arrays. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

PROGRAMMING FUNDAMENTALS

Lara Technologies Special-Six Test

PASS4TEST IT 인증시험덤프전문사이트

CS111: PROGRAMMING LANGUAGE II

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Class. Chapter 6: Data Abstraction. Example. Class

Lec 3. Compilers, Debugging, Hello World, and Variables

Conditionals, Loops, and Style

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

CS Week 5. Jim Williams, PhD

ITEC1620 Object-Based Programming. Lecture 13

Java Basic Programming Constructs

Computer Science is...

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Problem Grade Total

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Programming - 2. Common Errors

CS212 Midterm. 1. Read the following code fragments and answer the questions.

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

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

AP Computer Science Unit 1. Programs

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

Which code fragment must be inserted at line 6 to enable the code to compile?

Computer Science is...

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Loops and Expression Types

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Chapter 4: Control Structures I

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Loops. CSE 114, Computer Science 1 Stony Brook University

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Lecture 5: Methods CS2301

CSE 114 Computer Science I

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

CMP 326 Midterm Fall 2015

INFO Object-Oriented Programming

In Java there are three types of data values:

*Java has included a feature that simplifies the creation of

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Lab5. Wooseok Kim

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

Conversions and Overloading : Overloading

Write the letter of the correct answer for each problem in the blanks on the answer form.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

CPE Summer 2015 Exam I (150 pts) June 18, 2015

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Lab 14 & 15: String Handling

I. True/False: (2 points each)

Unit 10: Sorting/Searching/Recursion

Inheritance (Part 5) Odds and ends

CS111: PROGRAMMING LANGUAGE II

SUN Upgrade for the Sun Certified Java Programmer. SE 6.0. Download Full Version :

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Java Basics Programming Fundamentals

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

CSC 1214: Object-Oriented Programming

Manual for Basic Java

SUN Sun Certified Associate for the Java Platform.

CS251L REVIEW Derek Trumbo UNM

String. Other languages that implement strings as character arrays

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Chapter 3 Classes & Objects

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Huawei Test 5. Explanation: P.W. of Rs. 12,880 due 8 months hence=rs. [ (12880*100)/(100+(18*(8/12)))]

First Java Program - Output to the Screen

COMPUTER APPLICATION

1.Which four options describe the correct default values for array elements of the types indicated?

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

Class, Variable, Constructor, Object, Method Questions

CMPT 125: Lecture 3 Data and Expressions

WES-CS GROUP MEETING #9

Fall 2017 CISC124 9/16/2017

M e t h o d s a n d P a r a m e t e r s

For example, when your method is added to the class with the main method below, the output should be:

BM214E Object Oriented Programming Lecture 4

Building Java Programs

CPSC 219 Extra review and solutions

CS 149. Professor: Alvin Chao

Transcription:

1 Chapter 4 Arrays & Strings Question 1: Which of the following signatures are valid for the main() method of an application? [] A) public static void main(string vec[]) [] B) public static void main(string array[]) [] C) public void static main(string vec[]) [] D) public void static main(string array[]) [] E) static void main(string args[]) Question 2: Which of the following signatures are valid for the main() method of an application? [] A) public static void main(string []args) [] B) public static void main(string args[]) [] C) public static void main() [] D) public static void main(strings args[][]) [] E) void public static main(string args[])

2 Question 3: Which of the following signatures are valid for the main() method of an application? [] A) public void static main(string vec[]) [] B) public static void main(string args[]) [] C) public void static main(string [] vec) [] D) private void static main(string array[]) [] E) static public void main(string args[]) Question 4: Which of the following signatures are valid for the main() method of an application? [] A) public static void main(string []) [] B) public static void main(string []args) [] C) public void static main(string vec[]) [] D) public void static main() [] E) public static void mainmethod(string args[])

3 Question 5: Consider the following line of code: int vec[] = new int[10]; After this line is executed, which statement or statements are true? [] A) vec[10] is 0 [] B) vec[9] is 0 [] C) vec[0] is 0 [] D) veclength is 10 [] E) vec[1] is null Question 6: Regarding the following fragment of code, what is the output? 1 int index = 2; 2 String [] vec = { Avy, David, Ruty, Simon, Baruch, Yoel, Ronit, Tal, Tomer }; 3 vec[--index]+= # ; 4 for (int counter=index; counter < veclength; counter++) 5 { 6 Systemoutprintln(vec[counter]); 7 }

4 () A) The output includes David# () B) The output includes Ruty# () C) The output includes David# and Ruty# () D) The output includes Avy# () E) The output includes Avy# and David# Question 7: Regarding the following fragment of code, 1 int index = 2; 2 String [] vec = { A, B, C, D, E, F, G, H, I, J, K, L, M }; 3 vec[++index]+= # ; 4 for (int counter=index; counter < veclength; counter++) 5 { 6 Systemoutprintln(vec[counter]); 7 } () A) An ArrayIndexOutOfBoundsException is thrown () B) The output includes A# () C) The output includes B# and A# () D) The output includes C# () E) The output includes D#

5 Question 8: True or False: In order to add capabilities to the String object it is possible to declare a new class that extends the String class () A) true () B) false Question 9: Regarding the following fragment of code: 1 String str1 = abba ; 2 String str2 = new String( abba ); 3 if (str1==str2) 4 Systemoutprintln( Amsterdam ); 5 if (str1equals(str2)) 6 Systemoutprintln( Tel-Aviv ); The output will include: [] A) Amsterdam [] B) Tel-Aviv [] C) The compilation won t succeed [] D) The compilation will succeed but nothing will be printed on the screen

6 Question 10: Regarding the following fragment of code: 1 String str1 = abba ; 2 String str2 = abba ; 3 if (str1==str2) 4 Systemoutprintln( Amsterdam ); 5 if (str1equals(str2)) 6 Systemoutprintln( Tel-Aviv ); The output will include: [] A) Amsterdam [] B) Tel-Aviv [] C) The compilation won t succeed [] D) The compilation will succeed but nothing will be printed on the screen Question 11: Regarding the following fragment of code: 1 StringBuffer str1 = new StringBuffer( abba ); 2 StringBuffer str2 = new StringBuffer( abcd ); 3 StringBuffer str3 = str2append( Haifa ); 4 if (str2==str3) 5 Systemoutprintln( Paris ); 6 if (str1==str2) 7 Systemoutprintln( Amsterdam ); 8 if (str1equals(str2))

7 9 Systemoutprintln( Tel-Aviv ); The output will include: [] A) Amsterdam [] B) Tel-Aviv [] C) The compilation won t succeed [] D) The compilation will succeed but nothing will be printed on the screen [] E) Paris Question 12: Regarding the following fragment of code: 1 String str1 = abba ; 2 StringBuffer str2 = new StringBuffer( abba ); 3 StringBuffer str3 = str2append( Haifa ); 4 if (str2==str3) 5 Systemoutprintln( Paris ); 6 if (str1==str2) 7 Systemoutprintln( Amsterdam ); 8 if (str1equals(str2)) 9 Systemoutprintln( Tel-Aviv );

8 The output will include: [] A) Amsterdam [] B) Tel-Aviv [] C) The compilation won t succeed [] D) The compilation will succeed but nothing will be printed on the screen Question 13: True or False: Regarding the following code the reference that sbf variable holds doesn t change 1 StringBuffer sbf = new StringBuffer( trooper ); 2 StringBuffer temp = sbfreverse(); () A) true () B) false Question 14: Given the code below, 1 class Funky 2 { 3 int getnumbers(int num)[] 4 { 5 int vec[] = new int[10];

9 6 for(int index=1; index<10; index++) 7 vec[index] = index*num; 8 return vec; 9 } 10 } [] A) Line number 3 will cause a compilation error [] B) The code will compile but during its execution an exception will be thrown at line 3 [] C) The code will compile and run successfully Question 15: True or False: It is possible to specify the size of the created array with a variable rather than a literal () A) true () B) false Question 16: Given the code below, 1 int [] vec = {10,11,12,13,14,15,16,17,18}; 2 int num = 1; 3 vec[num] = num = 0; which of the following sentences (one or more) is true? [] A) The value of num is 1

10 [] B) The value of vec[0] is 10 [] C) The value of vec[1] is 11 [] D) The value of num is 0 [] E) The value of vec[1] equals to num Question 17: Given the code below, 1 public class TryIntern 2 { 3 public static void main(string args[]) 4 { 5 String str1 = new String("abc"); 6 String str2 = new String("abc"); 7 if (str1==str2) 8 Systemoutprint("Hello "); 9 str1intern(); 10 str2intern(); 11 if (str1==str2) 12 Systemoutprintln("Israel"); 13 } 14 } The output will be : [] A) Hello Israel

11 [] B) Hello [] C) Israel [] D) nothing (nothing will be printed on the screen) [] E) a compilation error will happen at line 9 Question 18: True or False: Given the code below, 1 public class TryIntern 2 { 3 public static void main(string args[]) 4 { 5 String str1 = "abc"; 6 String str2 = new String("abc")intern(); 7 if (str1==str2) 8 Systemoutprintln("Hello Israel"); 9 } 10 } The output will be : Hello Israel () A) True () B) False

12 Question 19: Given the code below, 1 public class TryIntern 2{ 3 public static void main(string args[]) 4 { 5 String str1 = new String("abc")intern(); 6 String str2 = new String("abc")intern(); 7 if (str1==str2) 8 Systemoutprint("Hello "); 9 str1intern(); 10 str2intern(); 11 if (str1==str2) 12 Systemoutprintln("Israel"); 13 } 14 } The output will be : [] A) Hello Israel [] B) Hello [] C) Israel [] D) nothing (nothing will be printed on the screen) [] E) a compilation error will happen at line 9

13 Question 20: True or False: The trim() method that belongs to the String class returns the string that results from removing the white space characters from the beginning and from the ending of the current string () A) True () B) False Question 21: True or False: Comparing using the equals method an object that belongs to the StringBuffer class to an object that belongs to the String class will always return false () A) True () B) False

14 Question 22: Given the code fragment below: 1 String str1 = new String( Shalom ); 2 String str2 = str1; 3 String str3 = new String(str1); 4 String str4 = str3; 5 str4 += Israel ; 6 if (str3==str4) 7 Systemoutprintln( one ); 8 if (str3equals(str4)) 9 Systemoutprintln( two ); 10 if (str1==str2) 11 Systemoutprintln( three ); 12 if (str3equals(str2)) 13 Systemoutprintln( four ); Which of the following will be included in the output (one ore more or none of them)? [] A) one [] B) two [] C) three [] D) four [] E) The code won t compile

15 Question 23: Given the code below, 1 public class Try 2 { 3 public static void main(string args[]) 4 { 5 Dot vec1[] = {new Dot(2,3), new Dot(4,5), new Dot(12,14)}; 6 Dot vec2[] = new Dot[3]; 7 Systemarraycopy(vec1,0,vec2,0,vec1length); 8 vec1[0]x = vec1[0]y = 1; 9 vec2[1]x = vec2[1]y = 1; 10 Systemoutprintln(vec2[0]x+vec1[1]y); 11 } 12 } 13 class Dot 14 { 15 public int x,y; 16 public Dot(int a, int b) 17 { 18 x = a; 19 y = b; 20 }

16 21 } The output will be : () A) 2 () B) 11 () C) 5 () D) 7 () E) The code will execute successfully but the output isn t any of the above answers Question 24: True or False: When dealing with arrays of objects, the Systemarraycopy() method will copy references and won t create new objects () A) True () B) False

17 Question 25: Given the code below, class Try1 { public static void main(string args[]) { String str1 = "abba"; StringBuffer str2 = new StringBuffer("abba"); StringBuffer str3 = str2append(" Haifa"); if (str2==str3) Systemoutprintln("Paris"); if (str1equals(str2)) Systemoutprintln("Tel-Aviv"); } } The output will be: () A) Paris () B) Tel-Aviv () C) The code won t compile () D) Nothing will be printed out on the screen

18 Question 26: Given the code below, public class Try { public static void main(string args[]) { Dot vec1[] = {new Dot(1,1), new Dot(2,2), new Dot(3,3)}; Dot vec2[] = new Dot[3]; Systemarraycopy(vec1,0, vec2,0, vec1length); vec1[0]x = vec1[0]y = 2; vec2[1]x = vec2[1]y = 2; Systemoutprintln(vec2[0]x+vec1[1]y); } } class Dot { public int x,y; public Dot(int a, int b) { x = a; y = b; }

19 } The output will be : () A) 2 () B) 11 () C) 5 () D) 7 () E) The code will execute successfully but the output isn t any of the above answers Question 27: Given the code below, int [] vec = {10,11,12}; int num = 1; vec[1] = 1; vec[num] = num = 0; which of the following sentences (one or more) is true? [] A) The value of num is 1 [] B) The value of vec[0] is 10 [] C) The value of vec[1] is 11

20 [] D) The value of num is 0 [] E) The value of vec[1] equals to num Question 28: True or False: It is possible to change the string which a String object represents () A) True () B) False Question 29: True or False: Consider the following line of code: int vec[] = new int[10]; After this line is executed, veclength is 9 () A) True () B) False

21 Question 30: Given the following code: int vec[]={1, 2, 3, 4}; int sum = 10; for(int index=0; index<veclength; index++) { sum += vec[index]; } sum -= 10; Systemoutprintln(sum); the output will be: () A) 10 () B) 11 () C) 12 () D) 6 () E) 20

22 Question 31: Given the following code: int vec[] = {1,2,3,4}; Systemoutprintln(veclength); the output will be: () A) 3 () B) 4 () C) 5 () D) 6 () E) 7

23 Question 32: Given the following code: String str = abcd ; Systemoutprintln(strlength); () A) The code doesn t compile () B) The output is: 4 () C) The output is: 5 () D) The output is: 3 Question 33: Given the following code: String [] vec, tavs; vec = new String[4]; tavs = new String[4]; vec[0] = abc ; vec[1] = def ;

24 vec[2] = ghi ; vec[3] = jkl ; tavs[0] = a ; tavs[1] = b ; tavs[2] = c ; tavs[3] = d ; vec = tavs; Systemoutprintln(vec[1]+vec[2]+vec[3]); the output is: () A) abcdefghi () B) abc def ghi () C) defghijkl () D) def ghi jkl () E) bcd () F) abc () G) a b c () H) b c d

25 Question 34: Given the following code: boolean vec[] = {false,false,boolean,0,1}; Systemoutprintln(veclength); the output will be: () A) 5 () B) 4 () C) 3 () D) null () E) This code doesn t compile

26 Question 35: Given the following code: int vec[5] = {1,2,3,4}; Systemoutprintln(veclength); the output will be: () A) 3 () B) 4 () C) 5 () D) 6 () E) This code doesn t compile Question 36: Regarding the following fragment of code: 1 String str1 = abba ; 2 String str2 = abba ; 3 str1 = str2touppercase();

27 4 if (str1==str2) 5 { 6 Systemoutprintln( Amsterdam ); 7 } 8 if (str1equals(str2)) 9 Systemoutprintln( Tel-Aviv ); The output will include: [] A) Amsterdam [] B) Tel-Aviv [] C) The compilation won t succeed [] D) The compilation will succeed but nothing will be printed out Question 37: Regarding the following fragment of code: 1 String str[3]; 2 str[0] = abba ; 3 str[1] = vcd ; 4 str[2] = edg ; 5 Systemoutprintln(str[0]+str[1]); The output will include: [] A) abbavcd

28 [] B) vcdedg [] C) The code doesn t compile [] D) The compilation succeeds but nothing will be printed out Question 38: Regarding the following fragment of code: 1 String str1 = new String( abba ); 2 String str2 = new String( abba ); 3 if (str1==str2) 4 Systemoutprintln( Amsterdam ); 5 if (str1equals(str2)) 6 Systemoutprintln( Tel-Aviv ); The output will include: [] A) Amsterdam [] B) Tel-Aviv [] C) The compilation doesn t succeed [] D) The compilation succeeds but nothing is printed out