首页 > 安全资讯 >

2011年计算机等级考试二级VB考点及习题(7)

11-09-09

F按值传递参数  形参和实参的类型不一定要统一,VB自动进行数据的转换。  –例如:Private Sub Form_Click()  Dim a As String : a = 2  Call sub1(a)  Print a  ...

 
 

  注意:

  F按值传递参数

  形参和实参的类型不一定要统一,VB自动进行数据的转换。

  –例如:Private Sub Form_Click()

  Dim a As String : a = 2

  Call sub1(a)

  Print a

  End Sub

  Private Sub sub1(ByVal b As Integer)

  b = b + 1 : Print b

  End Sub

  F按地址传递参数

  –当实参是变量时:如果形参规定了类型,则实参必须跟形参保持一致

  如果形参没有规定类型,则VB按要求进行数据类型转换

  –当实参是常量或表达式时:

  VB自动用“按值传递”的方式进行处理,如果数据类型不统一,VB自动进行数据转换

  试题

  1、在应用程序中用“Private function Fun(X as integer,Y as single)”定义了函数Fun。调用函数Fun的过程中的变量I、J均为Integer型,能正确引用函数Fun的是( )

  A.Fun(I,J) B.Call Fun(I,3.56) C.Fun(3.14,234) D.Fun("245","231.5")

  2.程序中的不同过程之间,不能通过_________进行数据传递。

  ①全局变量 ②窗体或模块级变量 ③形参与实参结合 ④静态变量

  A. ①②④ B. ①②③ C.②④ D.④

  二级考试必考题型:(课后习题)

  传值 传地址 Static变量 同名变量 递归过程

  1、以下有关变量作用域的说法中,错误的是( )

  A. 只有在标准模块中用public语句说明的变量才是全局变量

  B. 在过程中不能使用public语句说明全局变量

  C. 在标准模块的通用声明处可用private语句说明模块级变量

  D. 在窗体的通用声明处可用private语句说明窗体级变量

  2.一个名为frm1的窗体,在窗体通用声明处和过程sub1中分别定义:public k as integer、dim k as integer如何在sub1中访问全局变量k( )

  A.frm1.k B.form.k C.form1.k D.k

  3.阅读程序,给出输出结果:

  (1)字符串处理(数组参数)

  Option Base 1

  Private Sub Form_Click()

  Dim st As String, char As String

  Dim data() As String, i As Integer, j As Integer

  st = "10281018123811181038"

  For i = 1 To Len(st)

  If Mid(st, i, 1) <> "8" Then

  char = char & Mid(st, i, 1)

  Else

  j = j + 1

  ReDim Preserve data(j)

  data(j) = char

  char = ""

  End If

  Next i

  Call conver(data, char)

  Print char

  End Sub

  Private Sub conver(a() As String, ch As String)

  Dim i As Integer, j As Integer

  Dim n As Integer, dec As Integer

  For i = 1 To UBound(a)

  n = Len(a(i))

  For j = 1 To n

  dec = dec + Val(Mid(a(i), j, 1) * 8 ^ (n - j))

  Next j

  ch = ch & Chr(dec)

  dec = 0

  Next i

  End Sub

  (2)static变量

  Private Sub Command1_Click()

  Dim n As Integer, i As Integer

  n = 2

  For i = 9 To 1 Step -1

  Call sub2(i, n)

  Print i, n

  Next i

  End Sub

  Private Sub sub2(x As Integer, y As Integer)

  Static n As Integer

  Dim i As Integer

  For i = 3 To 1 Step -1

  n = n + x

  x = x - 1

  Next i

  y = y + n

  End Sub

  (3) 递归

  Private Sub Command1_Click()

  Dim a As Integer

  a = 2

  Call sub1(a)

  End Sub

  Private Sub sub1(x As Integer)

  x = x * 2 + 1

  If x < 10 Then

  Call sub1(x)

  End If

  x = x * 2 + 1

  Print x

  End Sub

  (4)递归

  Private Sub test(x As Integer)

  Dim i As Integer

  If x <> 0 Then

  Call test(x - 1)

  For i = 1 To x

  Print x;

  Next i

  Print

  End If

  End Sub

  Private Sub Form_Click()

  test 3

  End Sub

  (5)同名变量

  Dim y As Integer

  Private Sub Form_Click()

  Dim x As Integer,y as integer

  x=1 : y=1

  Print "x1=";x, "y1=";y

  Test

  Print "x4=";x, "y4=";y

  End Sub

  Private Sub Test()

  Dim x As Integer

  Print "x2=";x, "y2=";y

  x=2 : y=3

  Print "x3=";x,"y3=";y

  End Sub

  特例:

  1、运行程序在窗体显示的结果是:

  Private Sub Form_Click()

  Dim a As Integer, b As Integer

  a = 3: b = 4

  Call sub1(a, a)

  Print a, b

  Call sub1(b, b)

  Print a, b

  End Sub

  Private Sub sub1(x As Integer, y As Integer)

  x = x + 2

  y = x + y

  End Sub

  2、运行程序单击窗体,在窗体上显示的内容是:16 20

  Private Sub Form_Click()

  Dim n As Integer, m As Integer

  n = 1: m = 2

  Print n + m + f(n, m) ;

  '优先执行f函数,n,m变化后再运算 “+”

  n = 2: m = 1

  Print f(n, m) + f(m, n)

  'n,m第一次变化后,第二次以变化后的值再调用

  End Sub

  Private Function f(n As Integer, m As Integer)

  n = n + m

  m = m + 3

  f = n + m

  End Function

  3、执行下面的程序,在窗体上显示的输出结果是______;如果将A语句改成:Call sub1(b+1,a),则在窗体上显示的输出结果是______。

  Private Sub Form_Click()

  Dim a As Integer, b As Integer

  a = 1: b = 2

  Call sub1(a, b) 'A

  Print a, b

  End Sub

  Private Sub sub1(x As Integer, ByVal y As Integer)

  x = x + y

  y = x + y

  End Sub

相关文章
最新文章
热点推荐