0% found this document useful (0 votes)
26 views9 pages

Cpsc112A Asg2 Pratibha 10134087

This document outlines Assignment #2 for CPSC112A, focusing on data, expressions, and Java applications, with a total of 50 marks. It includes multiple choice questions and programming tasks requiring proper documentation and sample outputs. The assignment is due on March 13th, 2024, and specifies submission guidelines, including the creation of a zipped file containing all necessary materials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Cpsc112A Asg2 Pratibha 10134087

This document outlines Assignment #2 for CPSC112A, focusing on data, expressions, and Java applications, with a total of 50 marks. It includes multiple choice questions and programming tasks requiring proper documentation and sample outputs. The assignment is due on March 13th, 2024, and specifies submission guidelines, including the creation of a zipped file containing all necessary materials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CPSC112A - Introduction to Programming

Winter 2024

Assignment #2 – Data, Expressions, and Java Applications

Name and student #: Pratibha 10134087

Name and student #: Keshav 10126579


ASSIGNMENT MARK: ___ / 50 Due Date: Wednesday, Mar. 13th(before start of
class)

Submission: Submit your assignment to the instructor by email through Canvas Inbox

Complete the following programming questions, for each question:

 Ensure you have proper program documentation


 Ensure that each method is documented/explained
 Submit sample output of your program (via email)
 Submit the Java source code (via email)
 Ensure that each question starts with a Java Template

Do not wait until the last minute to do this assignment in case you run
into problems.

This assignment has:


3 programming questions, 10 marks each, 30 in total
10 multiple choice questions, 2 marks each, 20 in total

For programming questions, you may assume the user’s inputs are well-formatted.
A. Multiple Choice Questions [20 points, 2 points each]
1. Consider the following statements in Java.

int n = 0;
for (n = 1; n <= 20; n+=2)
{
n--;
}
What will be the value of ''n'' after these statements?

a. 19 b. 20 c. 21 d. 22

am/cpsc112A-Asg2-W24.docx Page 1/9


2. If you attempt to add an int, a byte, a long, and a double, the result will be a __________
value.

a. int
b. long
c. byte
d. double

3. What is the value in count after the following loop is executed?

int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
a. 11
b. 0
c. 9
d. 8
e. 10

4. Analyze the following code:

Code 1:
boolean even;

if (number % 2 == 0)
even = true;
else
even = false;

Code 2:
boolean even = (number % 2 == 0);

a. Code 2 has syntax errors.


b. Code 1 has syntax errors.
c. Both Code 1 and Code 2 have syntax errors.
d. Both Code 1 and Code 2 are correct, but Code 2 is better.

5. What is i after the following for loop?

int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}

a. 9 b. 10 c. undefined d. 11

am/cpsc112A-Asg2-W24.docx Page 2/9


6. Consider the following code fragment. What will be the output?

static int func(int[] arr, int b)


{
arr[0] = arr[0] + 10;
b = arr[0];
return b;
}
public static void main(String[] args)
{
int[] arr = {10, 20, 30};
int b = 1;
b = func(arr, b);
System.out.println(arr[0] + ", " + b);
}

a.20,1 b.10,1
c. 20, 20 d. None of the above

7. Consider the following piece of code:

public static int process2(int[] a)


{
a[1] = a[1] + 300;
int y = a[1] + 100;
return y;
}
public static void main (String[] args)
{
int[] twoInts = new int[2];
twoInts[0] = 0;
twoInts[1] = 10;
process2(twoInts);
System.out.println("" + twoInts[0] + "," + twoInts[1]);
}

What will be printed?

a. 0,10
b. 0,310
c. 0,410
d. None of the above

am/cpsc112A-Asg2-W24.docx Page 3/9


8. Consider the following code fragment. What will be the output?

static int func(int a)


{
if (a == 100)
return a;
return a + func(a + 1);
}
public static void main(String[] args)
{
int b = 1;
b = func(1);
System.out.println(b);
}
a. 1
b. 100
c. 5050
d. None of the above

9. In the following code, what is the printout for list2?

class Test {
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;

for (int i = 0; i < list2.length; i++)


System.out.print(list2[i] + " ");
}
}

a. 1 2 3 b. 1 1 1 c. 0 1 2 d. 0 1 3

am/cpsc112A-Asg2-W24.docx Page 4/9


10. Analyze the following code fragments that assign a boolean value to the variable even.

Code 1:
if (number % 2 == 0)
even = true;
else
even = false;

Code 2:
even = (number % 2 == 0) ? true: false;

Code 3:
even = number % 2 == 0;

a. Code 3 has a syntax error, because you attempt to assign number to even.
b. Code 2 has a syntax error, because you cannot have true and false literals in the conditional
expression.
c. All three are correct, but Code 1 is preferred.
d. All three are correct, but Code 2 is preferred.
e. All three are correct, but Code 3 is preferred.

B. Programming Questions [30 points, 10 points each]


a) Write a method to display a pattern as follows:

1
12
123
...
...
1 2 3 4 5 ... n

The method header is


public static void displayPattern(int n)

import java.util.Scanner;

public class Test {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// Enter the side of the pentagon


System.out.print("Enter n: ");
int n = input.nextInt();

displayPattern(n);
}

public static void displayPattern(int n) {


// Fill in the code here
}}

am/cpsc112A-Asg2-W24.docx Page 5/9


Solution a)

am/cpsc112A-Asg2-W24.docx Page 6/9


b) Write a static method that takes an array of integers as a parameter and returns true if the
count of strictly positive values is larger than the count of strictly negative values in the
array, false otherwise. A number is strictly positive if it is strictly larger than zero (i.e. it
cannot equal zero). A number is strictly negative if it is strictly less than zero.
Solution b)

c) Write a program Transpose.java to transpose a square two-dimensional integer array in


place without creating a second array. The transpose of a square two-dimensional array

am/cpsc112A-Asg2-W24.docx Page 7/9


(matrix) is created by:
• write the rows as the columns, or
• write the columns as the rows.

For example, the transpose of the matrix

is the following:

Your class Transpose should include the following method to do the transposition.
public static void transpose(int[][] arr)

Also, write a main() method in your Transpose class to test the method above. You need
initialize an array randomly and call the method.

Solution c)

am/cpsc112A-Asg2-W24.docx Page 8/9


N.B: Next, you are going to make sure you can use the submission properly. This will be how you turn
in your assignments electronically. The submission procedure only accepts zip files (NOT rar Files), so
you are going to create a “Asg2_YourFirstName_YourStudentID.zip” file containing all the programs
you just wrote as well as this document with your name and student number added to it. To create a
“.zip” file, find the directory where the files are saved and select all of them. If you right-click on one of
the selected files, you should have the option to create an archive by choosing “Send to” then
“Compressed “zipped” Folder”. Rename the created “.zip” file and give it the name
Asg1_YourFirstName_YourStudentID.zip.

10 marks will be deducted if:

1. Using more than one email for submission or


2. the email did not have a full name and a student # in the subject field or
3. the procedure for zipped file is not followed or
4. Java template and documentation are missing or
5. For all questions, sample outputs of your programs are not submitted or
6. This document, with your name and student number on it, is not included with the zipped file

A score of zero (0) will be given to every empty email.

am/cpsc112A-Asg2-W24.docx Page 9/9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy