Week 2

Look at the following class definition:

 public class Circle extends Shape {
    private float radius;
    private static int num = 0;

    public Circle(int xPos, int yPos) {
        this.xPos = xPos;
        this.yPos = yPos;
        num++;
    }
}

What is the value of num after the following code segment?

 for (int cnt = 2; cnt < 5; cnt++)
circles[cnt] = new Circle(cnt, cnt + 2);
  1. 0
  2. 1
  3. 2
  4. 3
  5. 4

 

 

Examine the following method:

 private void printThis(char ch, int num) {
    int i, j;
    for(i = 1; i <= num; i++) {
        for(j = 0; j < num - i; j++)
            System.out.print(" ");
        for (j = 0; j < i; j++)
            System.out.print(ch);
        System.out.println();
    }
}

What is the effect of calling the method (from within the same class) as follows:

 printThis('*', 2);
  1. One blank line.
  2. One line, containing "*".
  3. Two lines, the first with " *" and the second with "**".
  4. Two lines, each with a "*".
  5. A compilation error complaining about attempting to invoke a private method.

Week 3

Question1:

What is the output of running the following code?

 #include <stdio.h>

int y = 1;

void f(int a)
{
    a += y;
    y += a;
}

int main()
{
    int x = 0;

    f(x);

    printf("%d %d\n", x, y);

    return 0;
}

 

  1. 0 1
  2. 0 2
  3. 1 1
  4. 1 2
  5. none of the above
Question 2:

 

What is the output of running the following code?

 #include <stdio.h>

int a = 0;

void f()
{
    int a = 1;
    int b = 2;
    {
        int b = 3;
        int c = 4;
        {
            int c = 5;
            printf("%d %d %d", a, b, c);
        }
    }
}

int main()
{
    f();

    return 0;
}

 

  1. 0 2 4
  2. 1 2 4
  3. 1 3 4
  4. 1 2 5
  5. none of the above (should be 1 3 5)

Week 5

Given the following segment of code:


int colour;	
int *myColour;
myColour = &colour;
colour = 0;
/* Insert statement here! */

Which one of the following is reasonable output for the statement:

printf("%d, %d", &colour, myColour);

inserted at the place indicated in the code above?


  1. 0, 0
  2. 0, 68436812
  3. 68436812, 0
  4. 73423920, 68436812
  5. 68436812, 68436812



Given the following segment of code:


int colour;	
int *myColour;
myColour = &colour;
colour = 0;
/* Insert statement here! */

Which one of the following statements will print the rvalue of 'myColour'?


  1. printf("%d", colour);
  2. printf("%d", *colour);
  3. printf("%d", &colour);
  4. printf("%d", &myColour);
  5. printf("%d", *myColour);



Week 6
Q1)

The following diagram represents the contents of a small segment of memory. The variable 'ptr' initially points to the first 'cell' of memory in the diagram. The first 'cell' is found at memory location 1000.


'H' 'e' 'l' 'l' 'o' '!' \0 'W' 'o' 'r' 'l' 'd' '!' \0 '!'


What is printed by the following code segment?


char *ch;
ch = ptr+5;
while (*ch)
putchar(*ch++);


  1. o!
  2. !
  3. !World!
  4. o!World!
  5. None of the above.


Q2)

The following diagram represents the contents of a small segment of memory. The variable 'ptr' initially points to the first 'cell' of memory in the diagram. The first 'cell' is found at memory location 1000.


'H' 'e' 'l' 'l' 'o' '!' \0 'W' 'o' 'r' 'l' 'd' '!' \0 '!'


What is printed by the following statement?


printf("%s %s", ptr+5, ptr+12);

  1. ! !
  2. World! orld!
  3. Hello! World!
  4. World! World!
  5. None of the above.

Week 7

Q1)

Select the most accurate description of the effect of the following UNIX command:


mv File/fred File/freda

  1. Moves the named file into a different directory
  2. Changes the name of a file in directory 'File' from 'fred' to 'freda'
  3. Moves the file name 'File' from the directory 'fred' and put it in the directory 'freda'
  4. Moves the current working sub-directory from 'fred' to 'freda' within the directory 'File'
  5. Appends the letter 'a' to all filenames that start with the characters 'File' and end with the characters 'fred'


Q2)

Select the most accurate description of the effect of the following UNIX command:


cp ./test fred/

  1. Renames a directory called '.' to 'fred'
  2. Creates a copy of the file 'test' and names it 'fred'
  3. Puts a copy of the file 'test' into the directory 'fred'
  4. Copies the contents of a directory called '.' to a new directory called 'fred'
  5. Creates a sub-directory called 'test' and copies the files in the directory 'fred' into it


Week 9

Look at the following code:


typedef struct Node 
{
int val;
struct Node *next;
} Node;

Write some code that properly deletes all negative-valued elements of a linked list of 'Node' elements, pointed to by 'ptr'.


Node *tmp;
Node *prev;

for(; ptr != NULL; ptr = tmp)
{

    tmp = ptr->next;
    if(val < 0)
    {
        if(prev != NULL)
            prev->next = tmp;
        free(Node);
    }
    else
        prev = Node;
}


Week 10

Write some C code that will read words (one per line) from standard input.

Define a suitable structure that will hold each word and its length. Words can
be at most 20 characters. You must not waste space when storing the words.

Create a linked list of these structures.

Print out the third element of the list. If there are only two or less elements,
print "*none*".

#include <stdio.h>                                                                                             
#include <stdlib.h>                                                                                            
#include <string.h>                                                                                            

#define MAXLEN 20

typedef struct node
{                  
        char *string;
        struct node *next;
} Node;                   

int main(void)
{
        Node *head = NULL;
        Node *prev;
        char line[MAXLEN + 1];

        while (fgets(line, MAXLEN, stdin))
        {
            Node *node = NULL;
            int len = strlen(line);

            if((node = malloc(sizeof(Node))) == NULL)
            {
                fprintf(stderr, "Could not allocate memory for node.\n");
                exit(-1);
            }

            if((node->string = malloc(len + 1)) == NULL)
            {
                fprintf(stderr, "Could not allocate memory for string\n");
                exit(-1);
            }

            strncpy(node->string, line, MAXLEN + 1);

            if(head == NULL)
                head = node;
            else
                prev->next = node;

            prev = node;
        }

        if(head == NULL || head->next == NULL || head->next->next == NULL)
            printf("*none*\n");
        else
            printf("%s\n", head->next->next->string);

        return 0;
}

Week 13

Write a shell script that:

You may assume that all directories exist.

#!/bin/sh

for i in "$@"
do
    chmod u+w "$i/*"
done