請問下列C 語言程式執行後的輸出為何?(15 分)
#include <stdio.h>
int main(void)
{
int x = 0;
do {
switch (x) {
case 0: printf("%d\n", x);
case 1: x++; break;
case 2: printf("3\n");
case 3: printf("%d\n", x * 3);
case 4: x += 3; break;
default: x = x * 2;
printf("%d\n", x / 3);
}
} while(x < 10);
return (0);
}
語意分析(Semantics Analysis)對於程式語言的正確執行非常重要。
以下是Java 和C++程式,請說明程式編譯、執行結果與其原因,以及
程式指令之意義或影響。(18 分)
⑴Java 程式
static void test1(){
int n;
int [] x = new int[n];
}
⑵Java 程式
static void test2(){
int n=0;
int [] x = new int[n];
}
⑶Java 程式
static void test3(){
int n=0;
int [] x = new int[n];
x[0]=0;
}
⑷C++程式
void test4(){
int n;
int x[n];
}
⑸C++程式
void test5(){
int n=0;
int x[n];
x[0] =0;
}
⑹C++程式
void test6(){
int n=0;
int *x = new int[n];
x[0] =0;
}
請說明Java 與C++語言在陣列宣告上的語意分析的方法,與其優缺
點。(7 分)
有一函式如下,試問其平均時間複雜度為何?(25 分)
Function A(n) {
int a = 0;
int b = 0;
for (int i = 0; i < n; i++) {
a++;
}
printf("Hello");
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
b++;
}
}
printf("World");
}
閱讀以下Java 程式,列出數學式以說明變數e 在計算什麼?接著撰寫
遞迴(recursive)程式public static double etx(int accuracy, int x)
來計算前述變數e 的值,撰寫時必須使用etx 規定的參數與資料型
態。(25 分)
import java.util.Scanner;
public class EtoX
{
public static void main( String[] args )
{
Scanner input = new Scanner( System.in );
int number = 1;
int accuracy;
int factorial = 1;
int x;
double e = 1.0;
double exponent = 1.0;
x = input.nextInt();
accuracy = input.nextInt();
while ( number < accuracy )
{
exponent *= x;
factorial *= number;
e += exponent / factorial;
number++;
} // end while loop
System.out.printf( "x: %d%ne: %f%n", x, e );
} // end main
} // end class EtoX
請指出下列程式碼不合理之處。(25 分)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int s ,t;
scanf("%d%d", &s,&t);
while (s < 30) {
if (s < 100 || s >50) {
t++;
}
else if (s > 60 && s<-1) {
t++;
}
else if (s > 10 && s < 100) {
t--;
}
else
t++;
}
system("pause");
return 0;
}
下列為資料結構List 的Java 程式,而ListTest 為測試類別(class),
試回答以下問題:(35 分)
⑴ListTest 中”List<Integer> list = new List<>();”會先後呼叫那些
methods?傳送那些參數值?結果新物件list 的屬性值為何?
⑵執行ListTest.java 後會列印出什麼?
⑶撰寫public T removeFromBack() throws EmptyListException。
class ListNode<T>
{
T data;
ListNode<T> nextNode;
ListNode(T object)
{
this(object, null);
}
ListNode(T object, ListNode<T> node)
{
data = object;
nextNode = node;
}
T getData()
ListNode<T> getNext()
} // end class ListNode<T>
public class List<T>
{
private ListNode<T> firstNode;
private ListNode<T> lastNode;
private String name;
public List()
{
this("list");
}
public List(String listName)
{
name = listName;
firstNode = lastNode = null;
}
public void insertAtFront(T insertItem)
public void insertAtBack(T insertItem)
public T removeFromFront() throws EmptyListException
public T removeFromBack() throws EmptyListException
public boolean isEmpty()
public void print()
{
if (isEmpty())
{
System.out.printf("Empty %s%n", name);
return;
}
System.out.printf("The %s is: ", name);
ListNode<T> current = firstNode;
while (current != null)
{
System.out.printf("%s ", current.data);
current = current.nextNode;
}
System.out.println();
}
} // end class List<T>
public class EmptyListException extends RuntimeException
{
public EmptyListException()
{
this("List");
}
public EmptyListException(String name)
{
super(name + " is empty");
}
} // end class EmptyListException
public class ListTest
{
public static void main(String[] args)
{
List<Integer> list = new List<>();
try
{
list.insertAtFront(-1);
list.insertAtFront(99);
list.print();
int removedItem = list.removeFromFront();
removedItem = list.removeFromFront();
list.print();
removedItem = list.removeFromFront();
list.print();
}
catch (EmptyListException emptyListException)
{
emptyListException.printStackTrace();
}
}
} // end class ListTest
(99)
根據下列之C 語言程式,回答下列問題。(每小題5 分,共15 分)
若參數的傳遞方式是傳值(pass by value),程式執行所輸出數字為何?
若參數的傳遞方式是傳參考(pass by reference),程式執行所輸出數字
為何?
若參數的傳遞方式是傳值與結果(pass by value-result),程式執行所輸
出數字為何?
void add (int x, int y) {
x += x;
y += y;
}
void main() {
int a[2] = {1, 3};
add (a[0], a[1]);
printf (“%d %d\n”,
a[0], a[1]);
}
試問下列程式碼的輸出為何?(25 分)
#include <iostream>
using namespace std;
class A {
public:
int testa;
A() :testa(100) {
}
virtual void f() {
cout << "apple" << endl;
}
void g() {
cout << "banana" << endl;
}
};
class B : public A
{
public:
B() :testb(300) {}
void f() {
cout << "cat" << endl;
}
void g() {
cout << "dog" << endl;
}
int testb;
};
int main()
{
B b;
A *a = &b;
a->g();
a->f();
cout << a->testa << endl;
cout << b.testb << endl;
system("pause");
return 0;
}
(100)
(300)
下列程式Stack<T>繼承上題的List<T>。試撰寫Stack 中的建構子Stack
(),以及兩個主要methods: push(…)與pop()。(25 分)
public class Stack<T> extends List<T>
{
public Stack()
public void push(T object)
public T pop() throws EmptyListException
} // end class StackInheritance
以下是完整的Python 程式碼,請說明此程式的詳細功能為何?(10 分)
import os.path
file1 = input("Input a file name: ")
file2 = input("Input the other file name: ")
while not os.path.isfile(file1):
print("The file does not exist!")
file1 = input("Input a file name again: ")
while os.path.isfile(file2):
answer = input("The file existed, Overwrite it? y/n? ")
if answer == 'n' or answer=='N':
file2 = input("Input a file name again: ")
else:
break
fileObject1 = open(file1, "r")
fileObject2 = open(file2, "w")
content = fileObject1.read()
fileObject2.write(content)
fileObject1.close()
fileObject2.close()
請解釋程式語言中之變數動態範圍(Dynamic Scope)與語句參考環境
(Referencing Environment)。(6 分)
以下為一動態範圍之程式,請分別列出a, b, c 三點之參考環境。(9 分)
void subprogram1( ) {
int X, Y;
. . . <----------------- a
} /* end of subprogram1
void subprogram2( ) {
int Y, Z;
. . . <----------------- b
} /* end of subprogram2
void main( ) {
int W, Z;
. . . <----------------- c
} /* end of main
考慮下列與前後文無關的文法(context-free grammar)。請問它是LL(1)文法嗎?如果是,
請寫出它的LL(1)文法分析表(LL(1) parse table)。如果不是,請說明理由。(20 分)
1.S ::= ABC
2.A ::= dB
3.A ::= e
4.B ::= Af
5.B ::= g
6.C ::= h
請問下列C 程式執行的結果是什麼?(10 分)
#include <stdio.h>
int a, b;
void main() {
a = 4;
b = 30;
printf("Before the switch statement, a= %d.\n", a);
switch (a) {
case 1: a = a *10; break;
case 2: a = a *10; break;
case 3: a = a *10;
while (a < b ) case 4: { a = a + 5; } break;
case 5: a = a *10; break;
default:
b = a *10; break;
} // end of switch
printf("after the switch statement, a= %d.\n", a);
}
請檢視下面的C++程式,請問執行該程式後的輸出為何?(10 分)
#include <stdio.h>
class A{
public:
int X;
A() { X = 1; printf("X = %d\n", X); }
};
class B : public A {
public:
B() { X = 2; printf("X = %d\n", X); }
};
main()
{
B * pB = new B;
}
請宣告一個 Add class,內容包含以下特性:(25 分)
A.
Private member "A",型態為integer,並透過constructor 初始化為0。
B.
Private member "B",型態為integer,並透過constructor 初始化為0。
C.
Private method "print",將member "A"、"B"相加後顯示在console 中,且無回傳
值。
D.
Public method "add(int A, int B)",須達成以下行為:
i. 將參數A 的value 儲存到member "A"。
ii. 將參數B 的value 儲存到member "B"。
iii. 呼叫Private method "print",印出結果。
iv. 無回傳值。
public class Add {
// code here
}
試問下列C++程式碼執行後,其值為多少?(25 分)
#include <iostream>
using std::cout;
using std::endl;
int a(int i)
{
static int v=1;
int t=v;
v+=i;
return t;
}
int b(int i)
{
int v=1;
int t=v;
v+=i;
return t;
}
static int v;
int c(int i) {
int t=v;
v+=i;
return t;
}
int main()
{
int i;
for (i=1; i<=10; i<<=1)
{
a(i); b(i); c(i);
}
cout<<"a="<<a(i)<<endl;
cout<<"b="<<b(i)<<endl;
cout<<"c="<<c(i)<<endl;
return 0;
}
106年特種考試地方政府公務人員考試試題
全一張
(背面)
等
別: 三等考試
類
科: 資訊處理
科
目: 程式語言
傳值呼叫(call by value)與傳址呼叫(call by address)的差異為何?試以兩數對調
的問題撰寫程式說明之。(20 分)
一個簡單的分配陳述式(assignment statements)語法(grammar)如下:
上述的語法說明了分配陳述式的右邊是含有加法、乘法和小括弧的算數運算式(arithmetic
expressions),請使用最左演繹(leftmost derivation)完成下列的陳述式:(20 分)
A = B *(A + C)
觀察以下C 語言之程式,試問輸出為何?(20 分)
int main(){
int a = 1, b = 2, c = 3, d = 4;
printf( "%d\n%d\n%d\n%d\n", a+b+c+d, (b *= a),
(a += d), (d++));
}
試就Object Oriented language 與Functional language,回答下列問題:
比較兩種語言之不同與優缺點。(5 分)
續題,請對下列五種語言做歸類:C、Java、C++、Lisp、Prolog,並以下表作答
於試卷上(如果不屬於這兩類語言,請在None 打勾)。(10 分)
語言
Object Oriented language
Functional language
None
C
Java
C++
Lisp
Prolog
回答下列問題:(每小題8 分,共40 分)
請問下列C 程式片段的輸出為何?
int number = 60;
printf("%d\n", number << 1);
printf("%d\n ", number << 2);
printf("%d\n ", number << 3);
請問下列C 程式片段的輸出為何?
int x = 6688;
int *y = &x;
printf("%d\n", x);
x = 1357;
*y=6688;
printf("%d\n", x);
請問執行下列Java 程式片段後,someFunc(5)的值為何?
public static int someFunc(int n) {
return (n < 2) ? n : n * someFunc(n-1);}
請問下列Java 程式片段的輸出為何?
int x = 2; int y = 3; int z = 0;
if (x > 2) {
if (y > 2) {
z = x + y;
System.out.println("z is " + z); }
else
System.out.println("x is " + x); }
System.out.println("y is " + y);
請問下列Java 程式片段的輸出為何?
int x = 1;
int y = 5;
int z = x-- + (++y);
System.out.println("x = " + x + " y = " + y + " z = " +z);
假設類別A、B、C、D 的繼承關係如下:
class A; class B; class C: B; class D: C { A object;} main() {D objectD; },程式執行後,
請寫出解構函式的呼叫順序。(5 分)
九、考慮下列文法:S -> SS |(S)|()(每小題4 分,共12 分)
請指出此文法的terminal symbol、non-terminal symbol 及start symbol。
針對()()()字串,推導出left-most derivation sequence。
針對((( )))()字串,推導出right-most derivation sequence。