Final exam Flashcards
(135 cards)
What is printed by the following code?
int main () {
int array[5] = {10,30,50,70,90};
int p = array;
cout «_space;(p+1) + (&p) + 1; return 0;
}
41
What is the value in the variable Exam::total that is printed when this program is executed?
#include<iostream>
using namespace std;</iostream>
class Exam {
public:
static int total;
Exam() {
total++; }
};
int Exam::total = 0;
int main(){
Exam a, b, c;
Exam d, e, *f = new Exam;
cout «_space;Exam::total;
}
4
a delete instruction is needed before the main() ends.
Which of the following options is correct?
#include<iostream>
using namespace std;</iostream>
class MyClass {
public:
MyClass() {cout «_space;“MyClass constructed\n”;}
~MyClass() {cout «_space;“MyClass destroyed\n”;}
};
int main(){
MyClass * pt = new MyClass[3];
return 0;
}
delete [] pt;
The semantic structure of imperative programming languages normally include which of the following validations
parameters type in a function declaration should match these in the function call.
division by zero
statement should end with a ‘;’
unicity
a variable name should start with a letter, ‘$’ or ‘_’
type matching
parameters type in a function declaration should match these in the function call.
unicity
type matching
The syntactical structure of the imperative programming language reviewed in this course include which of the following validations
unicity
statement should end with a ‘;’
type matching
a variable name should start with a letter, ‘$’ or ‘_’
division by zero
parameters type in a function declaration should match these in the function call.
statement should end with a ‘;’
include<iostream></iostream>
Running the following program,
How many times the message “constructing” is printed on the screen?
using namespace std;
class Rectangle {
public:
Rectangle(){
cout «“constructing”«endl;
}
~Rectangle()
{cout «“destructing”«endl;
}
};
class Box : public Rectangle {
public:
Box(){
cout «“constructing”«endl;
}
~Box()
{cout «“destructing”«endl;
}
};
int main(){
Box * a = new Box[5];
delete[] a;
return 0;
}
10
Features of the logic paradigm includes
expressing computation in terms of logic predicates
using lambda calculus
classes and objects
expressing computation in terms of boolean expressions
expressing computation in terms of logic predicates
Which of the following options is the code in C++ for
A class Student that inherits from a class Person.
A constructor in Student that calls (is able to call) a constructor in Person
When the body of the method is not relevant to answer the question, it has been replaced for a comment // code
class Person {
public:
Person() {
//code
}
Person(char* lName, int year) {
// code
}
private:
char* lastName;
int yearOfBirth;
};
class Student : public Person {
public:
Student() {
//code
}
Student(char n, int y, char u):Person(n, y) {
//code
}
private:
char *university;
};
include<stdio.h></stdio.h>
What is printed on screen after running this program?
Be careful, do not add extra characters, not even spaces, unless they are part of your answer.
void main () {
int i = 0;
char a[] = “2020”;
while (a[i] != ‘\0’) {
(a + i) = (a + i) + 1;
i++;
}
char *q = a;
q[2]=’B’;
printf(“%s\n”, a);
}
31B1
The Java programming language is weakly typed
FALSE
What is printed on screen after running this code?
int main () {
int Employees[3][3] = {{10,20,30},{1,2,3},{15,25,35} };
cout «_space;Employees[2][1] - Employees[1][2] ;
return 0;
}
22
include<iostream></iostream>
What is printed after running this program?
using namespace std;
class GrandPa {
public:
virtual void foo() {
cout«“Hi, I am a GrandPa”«_space;endl;
}
};
class Dad : public GrandPa {
public: void foo() {
cout«“Hi, I am a Dad”«_space;endl;
}
};
class Kid : public Dad {
public: void foo() {
cout«“Hi, I am a Kid”«_space;endl;
}
};
int main(){
GrandPa * john = new Kid;
john -> foo();
delete john;
return 0;
}
Hi, I am a Kid
include<stdio.h></stdio.h>
What is printed by the following code?
int i=1;
void foo(int m, int *n){
printf(“i= %d m = %d n = %d\n”, i, m, *n);
i = 5; m = 3; *n = 4;
printf(“i= %d m = %d n = %d\n”, i, m, *n);
}
void main () {
int j = 2;
foo(j, &i);
printf(“i= %d j = %d\n”, i, j);
}
i = 1 m = 2 n = 1
i = 4 m = 3 n = 4
i = 4 j = 2
It is used to define an alternative name for an existing type:
enum
typedef
define
struct
typedef
Match the line with the error type (lexical, syntactic, or semantic) or with “correct” when there are no errors (lexical, syntactic, or semantic) in the line
int $ = 1; // in C/C++
char @ = ‘A’; // in C/C++
if = 5; // in C/C++
int If = (1<2 <3); // in C/C++
int If = (1<2 <3); // in Java
int x = 0/1; // Java
correct
Lexical Error
Syntactic error
correct
semantic
correct
include<iostream></iostream>
What is printed on screen when you run the following program?
using namespace std;
void recursive(int x) {
if (x<5) {
cout «_space;x;
recursive(x+1);
cout «_space;“B”;
}
else {
cout «_space;“A”;
}
}
int main(){
recursive(1);
}
1234ABBBB
Given the following struct:
struct contact { char name[32]; int phone; char email[32]; };
Which code can be used to create a contact and store data ?
struct contact x; scanf(“%s”, x.name); scanf(“%d”, &x.phone); scanf(“%s”, x.email);
What is printed on the screen after executing the lines below (in your personal computer)?
int x[4] = {1, 2, 3, 0}; printf(“%d”,sizeof x) ;
16
Lexical rules of Java and C/C++ languages include which of the following validations
unicity
division by zero
a variable name should start with a letter, ‘$’ or ‘_’
statement should end with a ‘;’
type matching
parameters type in a function declaration should match these in the function call.
a variable name should start with a letter, ‘$’ or ‘_’
Which of the following rules defines
“ if X is instructor of the course C and Y is enrolled in the course C then X teaches Y”
instructor(P,C), enrolled(S,C) :-teaches(P,S).
teaches(P,S) :- instructor(P,C); enrolled(S,C).
teaches(P,S) :- instructor(P,C), enrolled(S,C).
instructor(P,C); enrolled(S,C) :-teaches(P,S).
teaches(P,S) :- instructor(P,C), enrolled(S,C).
T or F
Logic programming describes what the problem is by a set of conditions and constraints, and leaves the computer to match the problem to the existing knowledge of facts and rules and to find solutions to the problem.
True
Which rules in Prolog match the following definition of a bad dog:
A dog is bad if it bites the postman, chews the newspaper, or chases the cat.
bad_dog(Dog) :- is_dog(Dog); bites(Postman). bad_dog(Dog) :- is_dog(Dog); chews(Newspaper). bad_dog(Dog) :- is_dog(Dog); chases(Cat).
is_dog(Dog), bites(Dog, Postman), is_postman(Postman):-bad_dog(Dog). is_dog(Dog), chews(Dog, Newspaper), is_newspaper(Newspaper):-bad_dog(Dog). is_dog(Dog), chases(Dog, Cat), is_cat(Cat):-bad_dog(Dog).
bad_dog(Dog) :- is_dog(Dog), bites(Dog, Postman), is_postman(Postman). bad_dog(Dog) :- is_dog(Dog), chews(Dog, Newspaper), is_newspaper(Newspaper). bad_dog(Dog) :- is_dog(Dog), chases(Dog, Cat), is_cat(Cat).
bad_dog(Dog) :- is_dog(Dog); bites(Postman); is_postman(Postman). bad_dog(Dog) :- is_dog(Dog); chews(Newspaper); is_newspaper(Newspaper). bad_dog(Dog) :- is_dog(Dog); chases(Cat); is_cat(Cat).
bad_dog(Dog) :- is_dog(Dog), bites(Dog, Postman), is_postman(Postman). bad_dog(Dog) :- is_dog(Dog), chews(Dog, Newspaper), is_newspaper(Newspaper). bad_dog(Dog) :- is_dog(Dog), chases(Dog, Cat), is_cat(Cat).
Write this statement as a Prolog clause.
If weather is cold, everyone likes it.
weather(cold) :-everyone(likes).
likes(weather, everyone):-cold(weather).
cold(weather), likes(X).
likes(X, weather) :- cold(weather).
likes(X, weather) :- cold(weather).
Write in Prolog the question “Which food is meal and lunch?”
meal(X); lunch(X).
meal(X). lunch(X).
meal(X):- lunch(X).
meal(X), lunch(X).
meal(X), lunch(X).